更新时间:2022-10-15 16:37:36
Your following line is causing the problem
while (!infile.eof())
As it depends on what is the size of the file, however your array size is fixed and(.i.e. 100 where you are storing the data).
You should use std::vector in your program as :
std::vector<Book> booklist;
while(/*check about infile>>x instead of !infile.eof()*/) {
Book tmp;
getline (infile, tmp.isbn);
getline (infile, tmp.title);
getline (infile, tmp.author);
infile >> tmp.price;
infile.get(dummy);
getline (infile, tmp.seller);
outfile << "ISBN: " << tmp.isbn << endl;
outfile << "Title: " << tmp.title << endl;
outfile << "Author: " << tmp.author << endl;
outfile << "Price: " << tmp.price << endl;
outfile << "Seller: " << tmp.seller << endl << endl;
//Add into the vector after filling the data into the
//struct Book instance tmp
booklist.push_back(tmp);
}
EDIT Yes, I we should not check the infile.eof() as this is not reliable way to identify the end of file. Instead of this we should check infile>>x. For more information please refer ISOCPP FAQ link on this: http://isocpp.org/wiki/faq/input-output#istream-and-eof