写入文件的最后 32 个字符

Write to File's Last 32 Characters

本文关键字:字符 最后 文件      更新时间:2023-10-16

我想写一个信息二进制文件的最后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();
}

希望你能帮忙,谢谢

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

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

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

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