fcntl函数能够改变已经打开文件的属性。
fcntl函数的的功能其实很复杂,它的功能取决于cmd这个参数。在获取(修改)已打开文件状态标志的时候,cmd这个参数取F_GETFL或F_SETFL
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
int main()
{
int fd = open("./1.txt",O_WRONLY);
if (-1 == fd)
{
perror("Open Failed");
exit(-1);
}
//一个汉字3字节大小
write(fd,"我们都是好孩子",21); //"我们都是好孩子会覆盖掉1.txt里原本的Hello World!"
//获取打开文件的属性。
int flag = fcntl(fd,F_GETFL,0);
if (-1 == flag)
{
perror("Fcntl Failed");
exit(-1);
}
//更改文件属性
flag += O_APPEND;
fcntl(fd,F_SETFL,flag);
//然后写入,就是追加写入。
write(fd,"——王筝\n",13);
write(fd,"流川枫与苍井空——黑撒乐队\n",40);
return 0;
}