C语言 使用 fopen 的附加模式覆盖文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3267820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overwrite file using append mode for fopen
提问by Elpezmuerto
I am using fopento 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.
由于遗留代码,我正在使用fopen写入二进制文件并使用该cstdio (stdio.h)库,并且它必须与 Windows 和 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.
对于原型,FILE * fopen ( const char * filename, const char * mode );我使用的是const char * mode = "ab",它将附加到一个二进制文件。写入操作在文件末尾追加数据。如果文件不存在,则创建该文件。
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.
我有 N 个输入文件,我在其中处理数据并写入每种类型的一个输出文件,其中我有 M 种类型。我处理一个输入文件并将数据写入每个相应的输出文件。然后我将关闭第 i 个输入文件并打开第 (i + 1) 个,并通过将输入文件中的数据附加到输出文件来重复该过程。
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)
如果可执行文件的开头存在输出文件,我希望将其删除。如果它存在并且我不删除它,那么当我使用该"wb"模式时,它只会将数据附加到输出文件中,这将导致我不想要的重复。我对 boost 解决方案持开放态度,我喜欢尽可能地保持标准(即尽可能避免 POSIX)
回答by sas4740
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.
“w”模式应该覆盖文件。它是“a”模式,可以避免删除已经存在的文件。
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.
编辑:remove (const char * filename)如果你想在执行开始时删除文件,你也可以。如果是这种情况,那么您永远不必使用“wb”模式。
回答by R Samuel Klatchko
One possibility would be to use open(_openfor Windows) to create the appropriate file handle and then use fdopen(_fdopenfor 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:
一种可能性是使用open(_open对于 Windows)来创建适当的文件句柄,然后使用fdopen(_fdopen对于 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");
回答by mipadi
If you want to overwrite rather than append, why not just use the mode "wb"? "w" overwrites the file when writing.
如果您想覆盖而不是追加,为什么不使用“wb”模式?“w”在写入时覆盖文件。

