What's wrong with the ifstream seekg

回眸只為那壹抹淺笑 提交于 2019-12-03 22:57:32

When ifs >> str; ends because the end of file is reached, it sets the eofbit.

Until C++11, seekg() could not seek away from the end of stream (note: yours actually does, since the output is Current pos: 0, but that's not exactly conformant: it should either fail to seek or it should clear the eofbit and seek).

Either way, to work around that, you can execute ifs.clear(); before ifs.seekg(pos);

It looks like in reading the characters it is hitting the EOF and marking that in the stream state. The stream state is not changed when doing the seekg() call and so the next read detectes that the EOF bit is set and returns without reading.

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