我的问题的要点是我正在读取一个文件,其中的字段由 # 分隔,但行尾除外。当我查看正在读入书籍结构的字段的打印语句时(使用 for 循环遍历整个数据文件/书籍列表(理论上)),它们都具有相同的值。例如,有 200 个完全相同的 id 后跟 200 个完全相同的标题,而不是在数据文件上往下读。
确切的代码是:
void loadTable(book table[], int size){
for (int i = 0; i < size; i++){
book newBook;
ifstream ifs("inventory.txt");
string bit;
getline(ifs, bit, '#');
newBook.bookId = atoi(bit.c_str());
cout << "BookID: " << newBook.bookId;
getline(ifs, bit, '#');
newBook.title = bit;
cout << "Title: " << newBook.title;
getline(ifs, bit, '#');
newBook.author = bit;
getline(ifs, bit, '#');
newBook.cost = atof(bit.c_str());
getline(ifs, bit, '#');
newBook.price = atof(bit.c_str());
getline(ifs, bit);
newBook.quantity = atoi(bit.c_str());
loadBook(table, newBook, size);
}
}
确切的结果:
tiesBookID: 116807Title: A Tale of Two CitiesBookID: 116807Title: A Tale of Two
CitiesBookID: 116807Title: A Tale of Two CitiesBookID: 116807Title: A Tale of Tw
o CitiesBookID: 116807Title: A Tale of Two CitiesBookID: 116807Title: A Tale of
Two CitiesBookID: 116807Title: A Tale of Two CitiesBookID: 116807Title: A Tale o
f Two CitiesBookID: 116807Title: A Tale of Two CitiesBookID: 116807Title: A Tale
of Two CitiesBookID: 116807Title: A Tale of Two CitiesBookID: 116807Title: A Ta
...