텍스트 파일을 읽을 때 C++을 선택하려고하는데 오류가 발생합니다. 왜 그런가?C++ 문제 텍스트 파일 읽기
입력 :
This is a test.
A test, with tabs and too many spaces.
If this is a good one,
then all will be well.
출력 :
then all will be well. too many spaces.
코드 :
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
ifstream infile ("A5.txt");
if (infile.is_open()) {
while (!infile.eof()) {
getline(infile,line);
cout << line << endl;
}
infile.close();
}
return 0;
}
어떤 OS/C++ 컴파일러입니까? – ybungalobill
OSX 10.7/Netbeans 7.2 –
'while (! infile.eof())'구조를 잃어 버렸습니다. "파스칼 병"이라고도합니다. C/C++에서 eof()는 파일 끝을 지나서 * 읽을 때까지 * true *가 아니므로 getline() *을 너무 많이 * 한 번 호출합니다. 'while (getline (infile, line))'은 그러한 루프를 작성하는 관용적 인 올바른 방법입니다. – arayq2