我如何超越现有文件的结尾?
假设我有 1000 字节的 test.bin
。
然后我跟着跑
std::filebuf fbuf;
fbuf.open("c:\\test.bin", std::ios_base::in | std::ios_base::out | std::ios_base::app | std::ios_base::binary);
fbuf.pubseekoff(10, std::ios_base::end);
fbuf.sputc(1);
fbuf.close();
运行此示例后,我希望新文件大小为 1011 字节。但它会在现有文件的末尾添加 0x01,使其成为 1001 字节。
有趣的是,当它是新文件时,它会按照我想要的方式工作。
我在 Windows 7 x64 上运行它,但目标平台将是 Ubuntu。
最佳答案
您设置了 std::ios_base::app
标志。这会导致所有写入都发生在文件末尾。如果要在当前位置执行写入,则不能在打开文件时指定此标志。
ios_base::app
Append file; that is, always write to the end of the file
关于c++ - 如何超越现有文件的结尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12851974/