【问题标题】:Write to File's Last 32 Characters写入文件的最后 32 个字符
【发布时间】:2013-03-17 18:41:48
【问题描述】:

我想将信息写入二进制文件的最后 32 个字符。但是当我调用我的 writeInfo 函数时,它会删除整个内容。我可以在写之前读取数据,但是在我用这个函数写之后,它会删除整个内容,而不是写。

void Info::writeInfo(char *filedestination)
{
ofstream test;
test.open(filedestination, ios::binary); 
test.seekp(-32, ios::end); // not sure about seekp
test.write(info, 31);
test.close();
}

希望你能帮忙,谢谢

【问题讨论】:

    标签: c++ file ofstream file-processing


    【解决方案1】:

    您必须以读写模式打开文件以防止调用open 截断它:

    test.open(filedestination, ios::binary | ios::in | ios::out); 
    

    【讨论】:

      【解决方案2】:

      http://www.cplusplus.com/reference/cstdio/fseek/

      尝试使用 fseek()。上面的链接是文档

      【讨论】:

      • 这是 seekp 的 C 等效项,他已经在使用它。混合 C 和 C++ 也是一个坏主意。
      猜你喜欢
      • 2011-04-22
      • 2011-08-26
      • 2020-08-19
      • 1970-01-01
      • 2012-11-12
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      相关资源
      最近更新 更多