C++ 覆盖写文件

写文件有三种模式:

  1. 截断写,文件打开之后立即清空原有内容
  2. 附加写,文件打开之后不清空原有内容,每次只能在文件最后写入
  3. 覆盖写,文件打开之后不清空原有内容,可以在文件任意位置写入

例如:文件原有内容为 123456,在开始位置覆盖写入 abcd,最后文件内容为 abcd56。首先使用下面的代码进行尝试:

#include <fstream>

int main()
{
    std::ofstream ofile("test.txt");
    ofile << "abcd";
}

输出的结果为 abcd,原有内容被截断了。可是并没有设置 std::ios::trunc 标志。ofstream 默认只设置了 std::ios::out。查阅 std::basic_filebuf<CharT,Traits>::open 发现单独的 std::ios::out std::ios::out | std::ios::trunc 等价。想要在不设置 std::ios::truncstd::ios::app 的情况下设置 std::ios::out,只能使用 std::ios::in | std::ios::out。因此修改代码为:

#include <fstream>

int main()
{
    std::fstream ofile("test.txt", std::ios::in | std::ios::out);
    ofile << "abcd";
}

成功输出 abcd56。覆盖写并没有被 C/C++ 标准直接支持,因此只能借助这种奇怪的组合实现。下面的写法也与其等价:

std::ofstream ofile("test.txt", std::ios::in);
ofile << "abcd";

std::ios::in | std::ios::out 和 C 语言的 r+ 等价,所有也可以这样写:

FILE* ofile = fopen("test.txt", "r+");
fprintf(ofile, "abcd");

在底层的 Linux 有更直接的办法:

#include <fcntl.h>
#include <unistd.h>

int main()
{
    int fd = open("test.txt", O_WRONLY);
    write(fd, "abcd", 4);
    close(fd);
}

这段代码只用了简单的 O_WRONLY 标志,就实现了覆盖写。

posted @ 2022-05-05 12:02  mkckr0  阅读(4080)  评论(0编辑  收藏  举报