所以这是我的文本文件:
计算机科学 4 345848534 1 伊万·伊万诺夫 格奥尔基·格奥尔基耶夫 普拉门安杰洛夫 ====== 奥利利 伦敦第五大道 44384208434 ***************** 生物学 2 58934673 0 格奥尔基·伊万诺夫 尼古拉·斯塔马托夫 杰克约翰逊 ====== 先头 Stanley Str 4, 曼彻斯特 449348344
这是我从文件中读取的函数:
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();
}
因此,我将构造“ApprovedBook”对象所需的信息与* **行分开。“=====”和星号之间的线条需要构造一个“制造商”对象,该对象也是 ApprovedBook 的属性。所以我读取了第一条信息并使用 << 输出对象(我已经为类预定义了运算符)。但在那之后,应用程序冻结并且似乎没有读取星星下方的下一条信息。那有什么问题?file.good() 条件是否足够或者可能需要一些更高级的检查?