C++ c++;-重用std::fstream

C++ c++;-重用std::fstream,c++,c++11,stream,C++,C++11,Stream,在我使用read方法读取结束std::fstream后,我无法seekg开始(tellg返回-1)。有没有一种方法可以在不关闭和重新打开它的情况下重用std::fstream if (!stream->is_open();) { throw "stream is closed"; } stream->seekg(0, stream->end); std::fstream::pos_type lenght = stream->tellg(); stream->

在我使用
read
方法读取结束
std::fstream
后,我无法
seekg
开始(
tellg
返回-1)。有没有一种方法可以在不关闭和重新打开它的情况下重用
std::fstream

if (!stream->is_open();)
{
    throw "stream is closed";
}
stream->seekg(0, stream->end);
std::fstream::pos_type lenght = stream->tellg();
stream->seekg(0, stream->beg);

if (0 != stream->tellg())
{
    throw "could not set seek to beginig";
}

char* buffer = new char[lenght];


stream->read(buffer, lenght);

*data = buffer;
return lenght;
第一次运行返回ok。
第二次运行抛出(无法搜索)。

一旦流进入故障模式(即设置了
std::ios\u base::failbit
),它将不会执行任何有用的操作,直到该标志被
clear()
ed。它可能足以使用

stream->clear();