so this is my text file:
Computer Science 4 345848534 1 Ivan Ivanov Georgi Georgiev Plamen Angelov ====== Oreily 5th avenue, London 44384208434 ***************** Biology 2 58934673 0 Georgi Ivanov Nikolay Stamatov Jack Johnson ====== Head first Stanley Str 4, Manchester 449348344
And this is my function to read from the file:
void ApprovedBook::read_from_file() {
fstream file;
string heading; int edition; long ISBN = 0L; bool isApproved = 0; // temp values for each book
string name; string address; long telephone = 0L; // temp values for manufacturer of the book
vector<string> authors; // temp vector for authors of the book
string line;
file.open("books.txt", ios::in | ios::app);
while (file.good()) {
for (int i=1; i<=NUM_ITEMS; i++) {
getline(file, line);
switch (i) {
case 1:
heading = line; break;
case 2:
edition = atoi(line.c_str()); break;
case 3:
ISBN = atol(line.c_str()); break;
case 4:
isApproved = (bool) atoi(line.c_str()); break;
}
}
getline(file, line);
while (line != "======") {
authors.push_back(line);
getline(file,line);
}
int i = 1;
getline(file, line);
while (line != "*****************") {
switch (i) {
case 1:
name = line; break;
case 2:
address = line; break;
case 3:
telephone = atol(line.c_str()); break;
}
getline(file, line);
i++;
}
Manufacturer m(name, address, telephone);
ApprovedBook a(heading, authors, edition, ISBN, m, isApproved);
cout << a << endl;
authors.clear();
}
file.close();
}
So I separate the information needed to construct an “ApprovedBook” object with the *** line. The lines between “=====” and the stars are needed to construct a “Manufacturer” object which is a property of ApprovedBook as well. So I read the first piece information and output the object with << (i have predefined the operator for the class). But after that the application freezes and doesn’t seem to read the next piece of information which is below the the stars. What’s the problem with that? Is the file.good() condition enough or maybe some more advanced check is needed ?
I replaced file.good with while (line != “”) and that worked fine for me.