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
그리고이 파일에서 읽어 내 기능입니다 :
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의 속성 인 "Manufacturer"개체를 구성하는 데 필요합니다. 그래서 첫 번째 조각 정보를 읽고 < < (나는 해당 클래스의 연산자를 미리 정의했습니다) 객체를 출력합니다. 그러나 그 후에 응용 프로그램은 얼어 붙어 별 아래에있는 다음 정보를 읽지 않는 것 같습니다. 그게 뭐가 문제 야? file.good() 조건이 충분합니까 아니면 좀 더 고급 검사가 필요합니까?
'ios :: in | ios :: app'은 의미가 없으며 플래그는 상호 배타적입니다. –
나는 ios ::를 남겨 뒀지 만, 아무것도 바뀌지 않았다. –
['while (! stream.eof())'및'while (stream.good())'는 거의 항상 잘못되었습니다] (http://www.parashift.com/c++-faq-lite/input-output. html # faq-15.5) (어디서 이런 것들이 계속 나옵니까?!?!?!!!!) –