【文件fd总结】系统调用的文件操作接口 | 文件描述符表&文件对象 | Linux底下一切皆文件

目录

1.系统调用的文件操作接口

1.1open

1.2write

1.3read_获取文件内容

1.4stat_获取文件属性

1.5close

2.文件fd


1.系统调用的文件操作接口

1.1open

int open(const char *pathname, int flags, mode_t mode);

//选项
O_WRONLY | O_CREAT
O_WRONLY | O_CREAT | O_TRUNC
O_WRONLY | O_CREAT | O_APPEND
O_RDONLY
 1: myfile.c 
  1 #include <stdio.h>                                          
  2 #include <unistd.h>  
  3 #include <sys/types.h>  
  4 #include <sys/stat.h>                   
  5 #include <fcntl.h>                      
  6                                         
  7 int main()                              
  8 {                                        
  9   umask(0);//动态设置当前进程的权限掩码   
 10   int fd = open("log.txt",O_WRONLY | O_CREAT,0666);//给出文件权限0666                              
 11   if(fd < 0)                            
 12   {                                     
 13     perror("open");                     
 14     return 1;                           
 15   }                                     
 16                                                                    
 17 }                  

1.2write

ssize_t write(int fd, const void *buf, size_t count);
//count是计算缓冲区的有效字符
 1: myfile.c 
  1 #include <stdio.h>
  2 #include <unistd.h>
  3 #include <sys/types.h>
  4 #include <sys/stat.h>
  5 #include <fcntl.h>
  6 #include<string.h>                                          
  7                                      
  8 int main()                           
  9 {                                      
 10   umask(0);//动态设置当前进程的权限掩码 
 11   int fd = open("log.txt",O_WRONLY | O_CREAT,0666);//给出文件权限0666                           
 12   if(fd < 0)                         
 13   {                                  
 14     perror("open");                  
 15     return 1;                        
 16   }                                  
 17                                             
 18   const char *message="hello linux file!\n";
 19   write(fd,message,strlen(message)); 
 20                                                               
 21 }                  

1.3read_获取文件内容

  • ope以读的方式打开文件。O_RDONLY
  • man 2 read
  • ssize_t read(int fd, void *buf, size_t count);
  • 第一个参数:从指定文件描述符fd读取。
  • 第二个参数:缓冲区(输出型参数)。读取的数据,会被放到buf里面,动态开辟的空间malloc。(用户传入参数,函数调用成功,这个参数被填满)
  • 第三个参数:期望读取多少(读取count个字节)
  • 返回值:>0 实际读取到的字节数  
  • =0 表示读取到文件结尾。
  1. 获取文件大小stat
  2. 以读的方式打开文件
  3. 开辟文件大小的空间的缓冲区
  4. 把文件的内容读取到缓冲区

 

1.4stat_获取文件属性

  • man 2 stat
  • int stat(const char *path, struct stat *buf);
  • int fstat(int fd, struct stat *buf);
  • int lstat(const char *path, struct stat *buf);
  • 此系统调用 是对文件的属性做操作,通过路径path或者文件描述符fd获取struct stat的结构体,获取指定文件的属性。
  • 返回值return val。如果函数成功,0被返回;失败,-1被返回和错误码被设置。

  • struct stat *buf是一种输出型参数

  • 用户传入一个结构体变量指针(struct stat*类型)struct stat st;(OS操作系统提供的内核数据结构直接用)

  • 函数调用成功之后,OS会把这个struct stat结构体中的内容填充完成。

 【struct stat】

【获取文件的st_size代表文件一共有多少个字节,文件的大小】 

 

1.5close

int close(int fd);

2.文件fd

  • fd文件描述符&文件描述符表&struct file文件对象&Linux底下一切皆文件

  • 文件描述符fd本质:内核的进程和文件映射关系的数据下标❗
  • 语言是系统调用的封装❗
  • task_strcut ☞ struct file_struct *files ☞ struct file_struct  ☞ struct file *fd_arry[N] ☞ struct file 
  • struct file☞文件相关属性+设备文件相关属性struct device
  • struct file☞包含设备操作方法的函数指针void (*read)(......) 函数 void k_read()
  • strcut file☞内核级缓存 
  • Linux底下一切皆文件:访问一个外设,只要找到这个设备的文件,直接调用函数指针指向的函数(操作方法),既可以访问设备了。同时也获取了设备文件的属性和文件的相关属性。

  • 30
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

唐唐思

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

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

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

打赏作者

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

抵扣说明:

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

余额充值