多个 `FILE` 附加到同一个文件
Multiple `FILE`s appending to the same file
POSIX and/or C 标准对多个 FILE
结构在追加模式下指向文件系统中的同一位置有何规定?具体来说,每个 FILE
将在每次写入后被刷新。例如,在这段代码中:
FILE *a = fopen("foo", "a");
FILE *b = fopen("foo", "a");
fputc('a', a);
fflush(a);
fputc('b', b);
fflush(b);
fclose(a);
fclose(b);
foo
的内容会一直是ab
,还是结果不确定?
此示例中的文件内容将始终为 ab
。
C 标准有这种语言:
7.21.5.3 The fopen
function
...
Opening a file with append mode (’a’ as the first character in the mode
argument) causes all subsequent writes to the file to be forced to the then current end-of-file, regardless of intervening calls to the fseek
function. In some implementations, opening a binary file with append mode (’b’ as the second or third character in the above list of mode
argument values) may initially position the file position indicator for the stream beyond the last data written, because of null character padding.
我认为这清楚地指定了发布的代码应该在执行后产生包含 ab
的文件,只要该文件之前不存在或为空并且文本文件没有奇怪的行为,例如自动附加换行符。
我文件是以二进制模式打开的,内容肯定是 ab
.
POSIX and/or C 标准对多个 FILE
结构在追加模式下指向文件系统中的同一位置有何规定?具体来说,每个 FILE
将在每次写入后被刷新。例如,在这段代码中:
FILE *a = fopen("foo", "a");
FILE *b = fopen("foo", "a");
fputc('a', a);
fflush(a);
fputc('b', b);
fflush(b);
fclose(a);
fclose(b);
foo
的内容会一直是ab
,还是结果不确定?
此示例中的文件内容将始终为 ab
。
C 标准有这种语言:
7.21.5.3 The
fopen
function...
Opening a file with append mode (’a’ as the first character in the
mode
argument) causes all subsequent writes to the file to be forced to the then current end-of-file, regardless of intervening calls to thefseek
function. In some implementations, opening a binary file with append mode (’b’ as the second or third character in the above list ofmode
argument values) may initially position the file position indicator for the stream beyond the last data written, because of null character padding.
我认为这清楚地指定了发布的代码应该在执行后产生包含 ab
的文件,只要该文件之前不存在或为空并且文本文件没有奇怪的行为,例如自动附加换行符。
我文件是以二进制模式打开的,内容肯定是 ab
.