문제

Suppose input.txt is 1 byte text file:

std::ifstream fin("input.txt", std::ios::in);
fin.get();              // 1st byte extracted
fin.get();              // try to extract 2nd byte
std::cout << fin.eof(); // eof is triggered
fin.unget();            // return back
std::cout << fin.eof(); // eof is now reset
fin.get();              // try to extract 2nd byte, eof assumed
std::cout << fin.eof(); // no eof is triggered

Seems like unget() breaks eof flag triggering also it breaks file pointers. Am I doing something wrong?

도움이 되었습니까?

해결책

eof is not set, but neither is good. The stream is ignoring operations because it's in a failure mode.

I cannot recall what unget is supposed to do after EOF, but unget goes right back into failure if I use clear to allow a retry.

http://ideone.com/JkDrwG

It's usually better to use your own buffer. Putback is a hack.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top