eof of istream in C++

放肆的年华 提交于 2019-12-01 20:20:19
J-16 SDiZ

As you see in comment, there is a new line:

ifstream ifs(argv[1]);
float f;
ifs >> f;
char c;
ifs.get( c ); // this will get '\n'

Even so, the EOF still won't set.... Read the paragraph you quoted again:

The function returns true if the eofbit stream's error flag has been set by a previous i/o operation. This flag is set by all standard input operations when the End Of File is reached in the sequence associated with the stream.

To get the eof bit set, you have to read PASS the eof. You can use peek() to do it if you want.

ifstream ifs(argv[1]);
float f;
ifs >> f;
char c;
ifs.get( c ); // this will get '\n'
ifs.eof();  // this is false;
ifs.peek();
ifs.eof(); // this is true

See also: istream::peek curious behavior wrt. EOF

The vim is going to add a new line at the end of the file. That is why EOF is not reached.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!