一、效果
追加写,每次调用 write() 写数据前,文件偏移量都会被定位到文件末尾,效果等同于调用了 lseek()
注意
文件偏移量的调整和写入操作会作为一个原子步骤执行
二、实操验证
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main()
{
const char *filename = "test.txt";
const char *content = "O_TRUNC\n";
int fd, len;
fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
if (fd == -1)
{
perror("open");
return EXIT_FAILURE;
}
len = strlen(content);
if (len != write(fd, content, len))
{
perror("write");
return EXIT_FAILURE;
}
close(fd);
return EXIT_SUCCESS;
}
$ cat test.txt
aaaaaaaa
$ ./O_APPEND_test
$ cat test.txt
aaaaaaaa
O_APPEND
$
727

被折叠的 条评论
为什么被折叠?



