2. 文件与I/O

2. 文件与I/o

  • open 系统调用
  • close 系统调用
  • creat 系统调用
  • read 系统调用
  • write 系统调用

open系统调用1

函数原型

  • 有几种方法可以获得允许访问文件的文件描述符。最常用的是使用open ()(打开)系统调用
  • 函数原型
    • *int open(const char path, int flags);
  • 参数
    • path :文件的名称,可以包含(绝对和相对)路径
    • flags:文件打开模式
  • 返回值
    • 打开成功,返回文件描述符;
    • 打开失败,返回-1

open例子

/* ************************************************************************                                                                                   
  2 > File Name:     02open.c
  3 > Author:        sansuitaibai
  4 > email:         2657302908@qq.com
  5 > Created Time:  2022年09月18日 14时33分42秒 CST
  6 > Description:   
  7  ************************************************************************/
  8 #include <sys/types.h>
  9 #include <sys/stat.h>
 10 #include <fcntl.h>
 11 #include<stdio.h>
 12 #include<unistd.h>
 13 #include<stdlib.h>
 14 #include<string.h>
 15 #include<errno.h>
 16 #define ERR_EXIT(m) \
 17     do{ \
 18         perror(m); \
 19         exit(EXIT_FAILURE); \
 20     }while(0)
 21 
 22 int main(){
 23 
 24     int fd = open("test.txt",O_CREAT | O_RDWR);
 25     if(fd == -1){
 26         ERR_EXIT("open");
 27 
 28     }
 29     printf("open succ\n");
 30     ERR_EXIT("open succ\n");
 31     return 0;
 32 
 33
 34 
 35 }

结果:

image

open系统调用2

函数原型

  • 函数原型
    • int open(const char *path, int flags,mode_t mode);
  • 参数
    • path :文件的名称,可以包含(绝对和相对)路径
    • flags:文件打开模式,见下表
    • mode:用来规定对该文件的所有者,文件的用户组及系统中其他用户的访问权限,见下表
      • mode 计算公式: mode & ^umask
  • 返回值
    • 打开成功,返回文件描述符;
    • 打开失败,返回-1
打开方式 描述
O_RDONLY 打开一个供读取的文件
O_WRONLY 打开一个供写入的文件
O_RDWR 打开一个可供读写的文件
O_APPEND 写入的所有数据将被追加到文件的末尾
O_CREAT 打开文件,如果文件不存在则建立文件
O_EXCL 如果已经置O_CREAT且文件存在,则强制open ()失败
O_TRUNC 在open ()时,将文件的内容清空

以上的标志在头文件<fcntl.h>

打开方式 描述
S_IRUSR 文件所有者的读权限位
S_IwUSR 文件所有者的写权限位
S__IXUSR 文件所有者的执行权限位
S_IRWXU S_IRUSR|S_IwUSR|\S_IXUSR
S_IRGRP 文件用户组的读权限位
S_IWGRP 文件用户组的写权限位
S_IXGRP 文件用户组的执行权限位
S_IRWXG S_IRGRP|S_IwGRP|S_IXGRP
S_IROTH 文件其他用户的读权限位
S_IWOTH 文件其他用户的写权限位
S_IXOTH 文件其他用户的执行权限位
S_IRWXO SIROTH|SIwOTH|S_IXOTH

以上在头文件<sys/stat.h>中定义。

例子

1 /* ************************************************************************                                                                                   
  2 > File Name:     02open.c
  3 > Author:        sansuitaibai
  4 > email:         2657302908@qq.com
  5 > Created Time:  2022年09月18日 14时33分42秒 CST
  6 > Description:   
  7  ************************************************************************/
  8 #include <sys/types.h>
  9 #include <sys/stat.h>
 10 #include <fcntl.h>
 11 #include<stdio.h>
 12 #include<unistd.h>
 13 #include<stdlib.h>
 14 #include<string.h>
 15 #include<errno.h>
 16 #define ERR_EXIT(m) \
 17     do{ \
 18         perror(m); \
 19         exit(EXIT_FAILURE); \
 20     }while(0)
 21 
 22 int main(){
 23 
 24     umask(0); //设置umask值,不让程序从shell那继承 
 25     int fd = open("03test.txt",O_CREAT | O_RDWR , 0666);
 26     if(fd == -1){
 27         ERR_EXIT("open");
 28 
 29     }
 30     printf("open succ\n");
 31     return 0;
 32 
 33 
 34 
 35 }

结果:
image

例子

1 /* ************************************************************************                                                                                   
  2 > File Name:     02open.c
  3 > Author:        sansuitaibai
  4 > email:         2657302908@qq.com
  5 > Created Time:  2022年09月18日 14时33分42秒 CST
  6 > Description:   
  7  ************************************************************************/
  8 #include <sys/types.h>
  9 #include <sys/stat.h>
 10 #include <fcntl.h>
 11 #include<stdio.h>
 12 #include<unistd.h>
 13 #include<stdlib.h>
 14 #include<string.h>
 15 #include<errno.h>
 16 #define ERR_EXIT(m) \
 17     do{ \
 18         perror(m); \
 19         exit(EXIT_FAILURE); \
 20     }while(0)
 21 
 22 int main(){
 23 
 24     umask(0); //设置umask值,不让程序从shell那继承 
 25     int fd = open("03test.txt",O_CREAT | O_RDWR  | O_EXCL, 0666);
 26     if(fd == -1){
 27         ERR_EXIT("open");
 28 
 29     }
 30     printf("open succ\n");
 31     return 0;
 32 
 33 
 34 
 35 }

结果:

image

注意标志 O_EXCL 的用法。

open调用的几点说明

  • 可以利用按位逻辑增加 (bitwise—OR)(|) 对打开方式的标志值进行组合。

    • 如:打开一个新的文件: #define NEWFILE (O_WRONLY | O_CREAT | O_TRUNC )
  • 对访问权限位进行访问所用到的标志符,均可以通过<sys/stat.h>访问到,同样可以通过 | 或运算进行组合

    • 如: #define MODE755 (S_IRWXU | S_IXGRP | S_XOTH)

close系统调用3

函数原型

  • 为了重新利用文件描述符,用close()系统调用释放打开的文件描述符
  • 函数原型:int close(int fd);
  • 函数参数:
    • fd :要关团的文件的文件描述符
  • 返回值
    • 如果出现错误,返回-1调用成功返回0

例子

在上面的例子中将打开的文件进行关闭即可。

ulimit -a查看系统的限制

image

creat系统调用4

  • 为了维持与早期UNIX系统的向后兼容性,Linux也提供可选的创建文件的系统调用,它称为:creat()
  • 函数原型:
    • int creat(const char* path , mode_t mode);
    • 参数:
      • path: 文件的路径和名称(可以是绝对路径或相对路径)
      • 用来规定对该文件的所有者,用户组,其他人的访问权限的设置
      • 返回值:打开成功,返回文件描述符,失败返回-1.

creat系统调用含义

  • 在UNIX早期版本中,open()系统调用仅仅存在两个参数的形式。
  • 如果文件不存在,它就不能进行打开文件
  • 文件的创建则是由单独的系统调用creat()完成。
  • 在Linux及所有的UNIX的近代版本中,creat()系统调用是多余的。

creat()调用:

  • fd = creat(file , mode);
  • 完全等价于: fd = open(file, O_WRONLY | O_CREAT | O_TRUNC , mode);
posted @   sansuitaibai  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示