gpt4 book ai didi

c++ - fstream -> seekp()/tellp() 将不起作用

转载 作者:行者123 更新时间:2023-11-28 07:33:11 25 4
gpt4 key购买 nike

每当我想将位置设置为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com