ifstream
을 사용하여 파일을 읽을 때 파일의 모든 줄을 반복하여 닫습니다. 그런 다음 동일한 ifstream
개체를 사용하여 다른 파일을 열려고 시도하면 여전히 파일 끝 오류가 발생합니다. 왜 파일을 닫아도 자동으로 상태를 지우지 않을지 궁금합니다. clear()
을 close()
다음에 분명히 호출해야합니다.파일을 닫으면 오류 상태가 자동으로 지워지지 않는 이유는 무엇입니까?
왜 이런 식으로 디자인 했나요? 다른 파일에 대해 fstream 객체를 재사용하고 싶다면 나에게 정말 고통 스럽다.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main()
{
ifstream input;
input.open("c:\\input.txt");
string line;
while (!input.eof())
{
getline(input, line);
cout<<line<<endl;
}
// OK, 1 is return here which means End-Of-File
cout<<input.rdstate()<<endl;
// Why this doesn't clear any error/state of the current file, i.e., EOF here?
input.close();
// Now I want to open a new file
input.open("c:\\output.txt");
// But I still get EOF error
cout<<input.rdstate()<<endl;
while (!input.eof())
{
getline(input, line);
cout<<line<<endl;
}
}
왜 출력^_ ^을 (를) 읽으시겠습니까? – alternative
@mathepic, 출력 파일은 항상 읽을 수 있지만 입력 파일을 쓸 수는 없습니다. 어쨌든 그 이름은 중요하지 않아야합니다 :) –
필자는 "input.txt"에 확실히 쓸 수 있으며 "output.txt"에서 읽을 수 있습니다.하지만 확실히 이상한 일이 아닌 것 같습니까? – alternative