I'm trying to read and write a sequence of int pairs from a file. The file would look something like this:
0 6
12 24
48 33
23 24
80 79
My goal is to read each pair into a struct:
struct foo {
int a;
int b;
}
And then push each struct into a stack. However, fstreams have proven rather difficult to handle for this task. Right now, my file read code looks like this:
std::fstream fileStream(file, std::ios::in);
int a, b;
while (fileStream >> a >> b) {
myStack.push({ a, b });
}
And my input might look like this (I have to do it individually because of what I'm using it for...):
inputFoo(foo bar) {
std::fstream fileStream(file, std::ios::out);
fileStream << bar.a << " " << bar.b;
}
However, I have a feeling this isn't how I should be efficiently and safely doing this. I also have a function that checks if the file exists already, but I'm not sure that one works either:
bool fileExists() {
std::ifstream stream;
return stream.good();
}
What's the best way to actually do this?