每当我想将位置设置为 X tellp return -1 时,fstream 中的 seekp() 有一个小问题。
代码:
fstream file("Riot.exe", ios::in | ios::binary | ios::out);
file.seekg(0, ios::beg);
...
if (...)
{
long pos;
file.seekp(882444);
pos = file.tellp();
std::cout << pos << std::endl; // pos = -1
}
结果是pos
返回-1,怎么办?
提前致谢!
最佳答案
要查找到文件末尾,将相对位置设置为 0,将基位置设置为 ios::end
:
if (...)
{
std::streampos pos;
file.seekp(0, ios::end); /*
^^^^^^^^^^^^^ */
pos = file.tellp();
std::cout << pos << std::endl;
}
关于c++ - fstream -> seekp()/tellp() 将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17261211/