
- /*
- #include
- #include
- int fcntl(int fd, int cmd, ... );
- 参数:
- - fd:需要操作的文件描述符
- - cmd:表示对文件描述符如何操作
- - F_DUPFD:复制文件描述符,复制的是参数fd,得到一个新的文件描述符
- - F_GETFL:获取指定的文件秒师傅文件状态fldg
- 获取的flag和我们通过open函数传递的flag是一个东西
- - F_SETFL:设置描述符的文件状态flag
- O_WRDR, O_RDONLY, O_WRONLY不可修改
- O_APPEND 表示追加数据
- NONBLOCK 设置成不阻塞
- 阻塞和非阻塞:描述的是函数的调用行为
- */
- #include
- #include
- #include
- #include
- int main() {
- int fd = open("1.txt", O_RDWR);
- if(fd == -1) {
- perror("open");
- return -1;
- }
-
- int flag = fcntl(fd, F_GETFL);
- if(flag == -1) {
- perror("fcntl");
- return -1;
- }
- flag |= O_APPEND;
- int ret = fcntl(fd, F_SETFL, flag);
- if(ret == -1) {
- perror("fcntl");
- return -1;
- }
-
- char* str = "nihao";
- write(fd, str, strlen(str));
- if(ret == -1) {
- perror("write");
- return -1;
- }
- close(fd);
- return 0;
- }