使用 fopen 的附加模式覆盖文件

发布于 2024-09-09 16:19:31 字数 721 浏览 10 评论 0原文

由于遗留代码,我正在使用 fopen 写入二进制文件并使用 cstdio (stdio.h) 库,并且它必须与 Windows 和 Linux 跨平台兼容。

对于原型 FILE * fopen ( const char * filename, const char * mode );,我使用 const char * mode = "ab",它将附加到一个二进制文件。写入操作将数据附加到文件末尾。如果文件不存在,则创建该文件。

我有 N 个输入文件,我在其中处理数据并将其写入每种类型的一个输出文件,其中我有 M 种类型。我处理一个输入文件并将数据写入每个相应的输出文件。然后,我将关闭第 i 个输入文件并打开第 (i + 1) 个输入文件,并通过将输入文件中的数据附加到输出文件来重复该过程。

如果输出文件存在于可执行文件的开头,我希望将其删除。如果它存在并且我不删除它,那么当我使用 "wb" 模式时,它只会将数据附加到输出文件中,这将导致我不想要的重复。 我对 boost 解决方案持开放态度,并且希望尽可能地保持标准(即,如果可能的话,避免使用 POSIX)

fopen C++ 参考

I am using fopen to write to a binary file and using the cstdio (stdio.h) library due to legacy code and it must be cross-platform compatible with Windows and Linux.

For the prototype, FILE * fopen ( const char * filename, const char * mode );, I am using const char * mode = "ab", which will append to a binary file. Writing operations append data at the end of the file. The file is created if it does not exist.

I have N number of input files where I process data from and write to one output file for each type, where I have M types. I process one input file and write the data to each corresponding output file. I then will close that ith input file and open (i + 1)th, and repeat the process by appending the data from the input file to the output files.

If an output file exists at the beginning on the executable, I want it deleted. If it exists and I don't delete it, then when I use the "wb" mode, then it will just append data to the output file, which will result in duplication I don't want. I am open to a boost solution and I like to maintain standards best as possible (i.e avoid POSIX if possible)

fopen C++ reference

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北城挽邺 2024-09-16 16:19:32

这是

char* infile[N] = //input names
char* outfile[M] = //output names

int i, j;
for (i = 0; i < N; i++){
  //process input
  char* mode = "ab";
  if (i == 0) mode = "wb";
  for (j = 0; j < M; j++){
    FILE* f = fopen(outfile[j], mode);
    //write to file
    fclose(f);
  }
}

“w”模式应该覆盖文件的一种方法。这是“a”模式,可以避免删除已经存在的文件。

编辑:如果您想在执行开始时删除文件,也可以remove (const char * filename)。如果是这种情况,那么您永远不必使用“wb”模式。

Here is one way

char* infile[N] = //input names
char* outfile[M] = //output names

int i, j;
for (i = 0; i < N; i++){
  //process input
  char* mode = "ab";
  if (i == 0) mode = "wb";
  for (j = 0; j < M; j++){
    FILE* f = fopen(outfile[j], mode);
    //write to file
    fclose(f);
  }
}

The "w" mode should overwrite the file. It is "a" mode that will avoid deleting a file that already exists.

EDIT: You can also remove (const char * filename) if you want to delete the files at the beginning of execution. If that's the case then you never have to use the "wb" mode.

金橙橙 2024-09-16 16:19:32

一种可能是使用 open (对于 Windows 为 _open)创建适当的文件句柄,然后使用 fdopen_fdopen) code> for Windows) 来创建一个 stdio 句柄。您将需要一些预处理器魔法来处理 Linux 和 Windows 中名称不完全相同的事实:

// Allow us to use Posix compatible names on Windows
#ifdef WINDOWS
#define open      _open
#define O_CREAT   _O_CREAT
#define O_TRUNC   _O_TRUNC
#define O_APEND   _O_APPEND
#define S_IREAD   _S_IREAD
#define S_IWRITE  _S_IWRITE
#define fdopen    _fdopen
#endif

int fd = open(filename, O_CREAT | O_TRUNC | O_APPEND, S_IREAD | S_IWRITE);
FILE *fp = fdopen(fd, "a");

One possibility would be to use open (_open for Windows) to create the appropriate file handle and then use fdopen (_fdopen for Windows) to create a stdio handle out of it. You will need some preprocessor magic to handle the fact that the names are not exactly the same in Linux and Windows:

// Allow us to use Posix compatible names on Windows
#ifdef WINDOWS
#define open      _open
#define O_CREAT   _O_CREAT
#define O_TRUNC   _O_TRUNC
#define O_APEND   _O_APPEND
#define S_IREAD   _S_IREAD
#define S_IWRITE  _S_IWRITE
#define fdopen    _fdopen
#endif

int fd = open(filename, O_CREAT | O_TRUNC | O_APPEND, S_IREAD | S_IWRITE);
FILE *fp = fdopen(fd, "a");
想念有你 2024-09-16 16:19:32

如果您想覆盖而不是追加,为什么不直接使用模式“wb”? “w”写入时覆盖文件。

If you want to overwrite rather than append, why not just use the mode "wb"? "w" overwrites the file when writing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文