读到文件末尾后开始搜索

seekg after reading to the end of the file

我尝试在 ifstream 上使用下面的代码两次 - 在我从文件中读取任何内容之前,以及在我读取到它的末尾之后(使用 readline())。

m_hexFile->m_ifsteam->seekg(0, m_hexFile->m_ifsteam->ios_base::end);
test1 = m_hexFile->m_ifsteam->tellg();
m_hexFile->m_ifsteam->clear();
m_hexFile->m_ifsteam->seekg(m_hexFile->m_startPosition);
test1 = m_hexFile->m_ifsteam->tellg();

(m_startPosition是文件中间的一些位置,对于这个问题不重要。)

在第一个 运行 上,代码按预期工作。然后当我第二次 运行 时,变量 test1 的值是-1,即使在使用 clear().

之后

我浏览了类似的问题,所有的解决方案都说使用函数 clear()(我已经这样做了)。

解决办法是检查别处。我在程序的其他地方关闭了流。在关闭的流中使用上面的代码当然是行不通的。