O_CREAT O_EXCL O_APPEND O_TRUNC

说明都是基于对文件/文件内容的操作。

① O_CREAT:若文件不存在则创建文件,🔺使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限。

🔺举例:在打开文件file1时,若文件不存在,就需要使用O_CREAT创建文件file1,在跟上文件访问权限mode。

fd = open("./file1",O_CREAT|O_RDWR,0600);

② O_EXCL: 如果同时指定了O_CREAT,而文件已经存在,则打开文件失败。     

🔺举例:在O_CREAT后跟上O_EXCL,如果已经有file1文件,就提示文件存在;如果没有file1文件,什么也不显示,并创建新的文件。

fd = open("./file1",O_CREAT|O_RDWR|O_EXCL,0600);
        if(fd == -1){
                printf("file1 exist!\n");
        }

③ O_APPEND: 添加新的文件内容到原有文件内容的后面。

🔺举例:事先在已创建的文件file1中写入12345678,O_CREAT后跟上O_APPEND后,结果就会在原文件内容12345678后换行跟上字符串“hello world!”

char *buf = "hello world!";                       
fd = open("./file1",O_RDWR|O_APPEND);
printf("open file1 success\nfd=%d\n",fd);
int n_write = write(fd,buf,strlen(buf));
printf("%d,%s\n",n_write,buf);

编译结果:
12345678
hello world! 

O_TRUNC:打开文件时,如果文件中本来是有内容的,而且为只读或只写打开成功,则将其长度截短为0(如果有新写入的内容,就将原内容覆盖掉)。

🔺举例:已知上一个文件file1中的内容是12345678(换行)hello world!,在使用了O_TRUNC后,就将原文件内容长度截短为0(清除),并生成新文件内容test!(新文件内容毫无痕迹的覆盖原文件内容)

char *buf = "test!";
fd = open("./file1",O_RDWR|O_TRUNC);
printf("open file1 success\nfd=%d\n",fd);
int n_write = write(fd,buf,strlen(buf));
printf("%d,%s\n",n_write,buf);

编译结果:
test!

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个示例程序,可以实现标准IO中的 (r, r+, w, w+, a, a+) ,文件名由 argv[1] 传入程序,同时使用文件 IO 操作 (O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_TRUNC, O_APPEND)。 ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <string.h> int main(int argc, char *argv[]) { if (argc != 2) { printf("Usage: %s filename\n", argv[0]); exit(1); } // 打开文件 int fd = open(argv[1], O_CREAT | O_RDWR, 0644); if (fd < 0) { perror("open file error"); exit(1); } // 测试打开方式 char mode[3]; printf("Enter mode (r, r+, w, w+, a, a+): "); scanf("%s", mode); // 根据打开方式设置文件指针位置 int whence; switch (mode[0]) { case 'r': whence = SEEK_SET; break; case 'w': whence = SEEK_SET; ftruncate(fd, 0); // 清空文件内容 break; case 'a': whence = SEEK_END; break; default: printf("Invalid mode\n"); exit(1); } // 根据打开方式设置读写权限 int oflag; switch (mode[0]) { case 'r': oflag = O_RDONLY; break; case 'w': oflag = O_WRONLY | O_TRUNC; break; case 'a': oflag = O_WRONLY | O_APPEND; break; default: printf("Invalid mode\n"); exit(1); } // 根据打开方式设置文件指针位置和读写权限 if (mode[1] == '+') { whence = SEEK_SET; oflag = O_RDWR; } // 使用文件 IO 操作打开文件 int fd2 = open(argv[1], oflag); if (fd2 < 0) { perror("open file error"); exit(1); } // 将文件指针设置到文件末尾 if (whence == SEEK_END) { lseek(fd2, 0, SEEK_END); } // 读取或写入文件内容 char buf[1024]; int n; switch (mode[0]) { case 'r': while ((n = read(fd2, buf, sizeof(buf))) > 0) { write(STDOUT_FILENO, buf, n); } break; case 'w': case 'a': while (fgets(buf, sizeof(buf), stdin) != NULL) { write(fd2, buf, strlen(buf)); } break; default: printf("Invalid mode\n"); exit(1); } // 关闭文件 close(fd); close(fd2); return 0; } ``` 你可以将上述代码保存为一个名为 `file_io.c` 的文件,然后使用以下命令编译并执行程序: ``` gcc file_io.c -o file_io ./file_io filename ``` 其中,`filename` 为你需要读写的文件名。执行程序后,根据提示输入打开方式,该程序会使用文件 IO 操作和指定的打开方式读取或写入文件内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

D.•

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值