我尝试使用查找和重新读取数据的方式,但代码失败了。
代码如下:
代码如下:
std::ifstream ifs (filename.c_str(), std::ifstream::in | std::ifstream::binary);
std::streampos pos = ifs.tellg();
std::cout <<" Current pos: " << pos << std::endl;
// read the string
std::string str;
ifs >> str;
std::cout << "str: " << str << std::endl;
std::cout <<" Current pos: " <<ifs.tellg() << std::endl;
// seek to the old position
ifs.seekg(pos);
std::cout <<" Current pos: " <<ifs.tellg() << std::endl;
// re-read the string
std::string str2;
ifs >> str2;
std::cout << "str2: (" << str2.size() << ") " << str2 << std::endl;
std::cout <<" Current pos: " <<ifs.tellg() << std::endl;
我的输入测试文件是
qwe
输出结果为
Current pos: 0
str: qwe
Current pos: 3
Current pos: 0
str2: (0)
Current pos: -1
有人能告诉我是什么问题吗?