认识基础I/O、文件系统

本文探讨了C语言中的文件操作接口,包括fopen、fclose、fwrite、fread等函数,并介绍了系统文件I/O、文件描述符(fd)的概念。文章还讨论了重定向的本质和规则,以及fd与FILE的区别。最后,文章解析了文件系统,特别是inode的含义和作用,以及硬链接和软链接的工作原理。
摘要由CSDN通过智能技术生成

回顾c语言中的文件操作接口

fopen(FILE* fp,char flag);
fclose(FILE* fp);
fwrite(char* msg,int len,size_t count,FILE*fp);
fread(void * msg,size_t size,size_t count,FILE* fp);
其中flag表示打开文件的方式,”w”(只写)”r”(只读)等。

  1 #include<stdio.h>
  2 #include<string.h>
  3 int main(){
  4     FILE* fp=fopen("myfile","w");
  5     if(!fp){
  6         printf("fopen error");
  7         return 1;
  8     }
  9     const char* msg="hello hello\n";
 10     int count=5;
 11     while(count--){
 12         fwrite(msg,strlen(msg),1,fp);
 13     }
 14     fclose(fp);
 15     return 0;
 16 }

这里写图片描述

系统文件I/O

> open         read                close                 write
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int open(const char* path,int flags);
int open(const char* path,int flags,mode_t mode);//mode是要设定的权限
int write(int fd,char* buf,int len);//从buf往fd中写
ssize_t read(int fd,buf,size_t size);
close(int fd);

示例:

  1 #include<stdio.h>
  2 #include<sys/types.h>
  3 #include<sys/stat.h>
  4 #include<fcntl.h>
  5 #include<unistd.h>
  6 #include<string.h>
  <
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值