I am trying to read from a simple text file in a specific order. In this case I read an int, then string, then double. Here is the text file:
54321 Television
250
46782 Laptop
1200
23461 Ipad
500
87612 Playstation
400
Here is the function I am using to check the file for errors (wrong types or wrong filename):
void Electronics::ReadData(istream& electronicsFile)
{
int barcode;
string name;
double price;
while (electronicsFile.good()) {
electronicsFile >> barcode >> name >> price;
}
if (electronicsFile.fail()) {
throw runtime_error("Error reading electronics file");
}
}
The issue that I am running into is that my text file ends with a newline, causing my program to throw the runtime error every time it reads the file to the end. How can I read the entire file and not have it return a failbit?