Is C ++ safe to disable on flutes

Suppose input.txt- 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

It seems like unget()breaks eoffires a flag and it breaks pointers to files. Am I doing something wrong?

+4
source share
1 answer

eofnot installed but not good. The stream ignores operations because it is in a failure mode.

I can’t remember what I ungetshould do after EOF, but it ungetgoes back to the error if I use clearit to allow a repeat.

http://ideone.com/JkDrwG

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

+3
source

Source: https://habr.com/ru/post/1534115/


All Articles