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

时间:2013-06-23 13:29:48

标签: c++ fstream

我在fstream中的seekp()有一点问题,只要我想将位置设置为X tellp return -1。

代码:

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,我该怎么办?

提前致谢!

1 个答案:

答案 0 :(得分:1)

要搜索文件的末尾,请将相对位置设置为0,将基准位置设置为ios::end

if (...)
{ 
    std::streampos pos;
    file.seekp(0, ios::end);                                                   /*
              ^^^^^^^^^^^^^                                                    */
    pos = file.tellp();
    std::cout << pos << std::endl; 
}