Having gone through several different questions that were about roughly the same thing, I've just decided to make my own question. Every time my code is run it says exited with code -107371819
and I can't quite figure out what's wrong with it. I know that this code is usually thrown when the program attempts to read uninitialized memory, but I'm not sure where that's happening in my code.
I'm attempting to read through all of the structures of type/size Tool
. It does successfully list all of them (not in the prettiest format) and successfully closes the file, however after this occurs the program throws that lovely read access error.
Here's the Tool structure:
struct Tool {
int tid; // "Tool identification number"
string name;
int quantity;
double cost;
};
This is the code that I've been attempting to run:
void listTools() {
// Iterates through the whole file while printing each Tool struct
fstream file;
file.open("hardware.txt", ios::binary | ios::in);
Tool temp = Tool();
while (!file.eof()) {
file.read((char*)&temp, sizeof(Tool)); // <-- I think this is my problem line
if (file.fail()) {
cout << "File failed to read, end of file has been reached." << endl;
break;
}
cout << temp.getTid() << temp.getName() << temp.getQuantity() << temp.getCost() << endl; // Again, not pretty
}
file.clear();
file.close();
cout << "File closed successfully." << endl;
// Access error happens here
}
The part that makes this a little weird is that it ran properly once, but I haven't changed anything from that point. Any help is appreciated!