当前位置: 首页 > news >正文

Linux——基础IO

在这里插入图片描述


📘北尘_:个人主页

🌎个人专栏:《Linux操作系统》《经典算法试题 》《C++》 《数据结构与算法》

☀️走在路上,不忘来时的初心

文章目录

  • 一、C语言IO
    • 1、写文件
    • 2、读文件
    • 3、stdin & stdout & stderr
  • 二、系统文件I/O
    • 1、写文件
    • 2、读文件
  • 三、接口介绍
  • 四、文件描述符fd
  • 五、文件描述符的分配规则
  • 六、重定向


一、C语言IO

1、写文件

在这里插入图片描述
在这里插入图片描述

#include <stdio.h>
#include <string.h>
int main()
{FILE *fp = fopen("myfile", "w");if(!fp){printf("fopen error!\n");}const char *msg = "hello bit!\n";int count = 5;while(count--){fwrite(msg, strlen(msg), 1, fp);}fclose(fp);return 0;
}

2、读文件

在这里插入图片描述

在这里插入图片描述

#include <stdio.h>
#include <string.h>
int main()
{FILE *fp = fopen("myfile", "r");if(!fp){printf("fopen error!\n");}char buf[1024];const char *msg = "hello bit!\n";while(1){//注意返回值和参数,此处有坑,仔细查看man手册关于该函数的说明ssize_t s = fread(buf, 1, strlen(msg), fp);if(s > 0){buf[s] = 0;printf("%s", buf);}if(feof(fp)){break;}}fclose(fp);return 0;
}

3、stdin & stdout & stderr

C默认会打开三个输入输出流,分别是stdin, stdout, stderr
仔细观察发现,这三个流的类型都是FILE*, fopen返回值类型,文件指针


二、系统文件I/O

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1、写文件

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{umask(0);int fd = open("myfile", O_WRONLY|O_CREAT, 0644);if(fd < 0){perror("open");return 1;}int count = 5;const char *msg = "hello bit!\n";int len = strlen(msg);while(count--){write(fd, msg, len);//fd: 后面讲, msg:缓冲区首地址, len: 本次读取,期望写入多少个字节的数 据。 返回值:实际写了多少字节数据}close(fd);return 0;
}

2、读文件

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{int fd = open("myfile", O_RDONLY);if(fd < 0){perror("open");return 1;}const char *msg = "hello bit!\n";char buf[1024];while(1){ssize_t s = read(fd, buf, strlen(msg));//类比writeif(s > 0){printf("%s", buf);}else{break;}}close(fd);return 0;
}

三、接口介绍

使用man2号手册

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
pathname: 要打开或创建的目标文件
flags: 打开文件时,可以传入多个参数选项,用下面的一个或者多个常量进行“或”运算,构成flags。
参数:O_RDONLY: 只读打开O_WRONLY: 只写打开O_RDWR : 读,写打开这三个常量,必须指定一个且只能指定一个O_CREAT : 若文件不存在,则创建它。需要使用mode选项,来指明新文件的访问权限O_APPEND: 追加写返回值:成功:新打开的文件描述符失败:-1

四、文件描述符fd

在这里插入图片描述

Linux进程默认情况下会有3个缺省打开的文件描述符,分别是标准输入0, 标准输出1, 标准错误2.
0,1,2对应的物理设备一般是:键盘,显示器,显示器

而现在知道,文件描述符就是从0开始的小整数。当我们打开文件时,操作系统在内存中要创建相应的数据结构来描述目标文件。于是就有了file结构体。表示一个已经打开的文件对象。而进程执行open系统调用,所以必须让进程和文件关联起来。每个进程都有一个指针*files, 指向一张表files_struct,该表最重的部分就是包涵一个指针数组,每个元素都是一个指向打开文件的指针!所以,本质上,文件描述符就是该数组标。所以,只要拿着文件描述符,就可以找到对应的文件


五、文件描述符的分配规则

直接看代码:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{int fd = open("myfile", O_RDONLY);if(fd < 0){perror("open");return 1;}printf("fd: %d\n", fd);close(fd);return 0;
}

发现输入3

关闭0或者2

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{close(0);//close(2);int fd = open("myfile", O_RDONLY);if(fd < 0){perror("open");return 1;}printf("fd: %d\n", fd);close(fd);return 0;
}

发现是结果是: fd: 0 或者 fd 2 可见,文件描述符的分配规则:在files_struct数组当中,找到当前没有被使用的最小的一个下标,作为新的文件描述符。


六、重定向

那如果关闭1呢?看代码:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{close(1);int fd = open("myfile", O_WRONLY|O_CREAT, 00644);if(fd < 0){perror("open");return 1;}printf("fd: %d\n", fd);fflush(stdout);close(fd);exit(0);
}

此时,我们发现,本来应该输出到显示器上的内容,输出到了文件 myfile 当中,其中,fd=1。这种现象叫做输出重定向。常见的重定向有:>, >>, <

那重定向的本质是什么呢?
在这里插入图片描述

使用 dup2 系统调用

在这里插入图片描述

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main() {int fd = open("./log", O_CREAT | O_RDWR);if (fd < 0) {perror("open");return 1;}close(1);dup2(fd, 1);for (;;) {char buf[1024] = {0};ssize_t read_size = read(0, buf, sizeof(buf) - 1);if (read_size < 0) {perror("read");break;}printf("%s", buf);fflush(stdout);}return 0;
}

http://www.lryc.cn/news/305277.html

相关文章:

  • 数据结构-数组
  • 【Java程序设计】【C00279】基于Springboot的智慧外贸平台(有论文)
  • C#,计算几何,计算机图形学(Computer Graphics)洪水填充算法(Flood Fill Algorithm)与源代码
  • C# 实现网页内容保存为图片并生成压缩包
  • C#_事件简述
  • C语言:指针(一)
  • 【leetcode刷题之路】面试经典150题(3)——哈希表+区间
  • 群晖NAS DSM7.2.1安装宝塔之后无法登陆账号密码问题解决
  • 9、使用 ChatGPT 的 GPT 制作自己的 GPT!
  • 企业微信应用开发:使用Cpolar域名配置进行本地接口回调的调试指南
  • js 可选链运算符(?.)空值合并运算符(??)逻辑空赋值运算符(??=)
  • vue 手势解锁功能
  • 介绍 CI / CD
  • Stable Diffusion 3 Early Preview发布
  • 【解决(几乎)任何机器学习问题】:特征选择
  • 24 双非计算机秋招总结
  • 用友NC65与用友NCC对接集成NC65-凭证列表查询打通凭证新增
  • 【初中生讲机器学习】12. 似然函数和极大似然估计:原理、应用与代码实现
  • 【达梦数据库】查看pesg回滚段信息的视图和SQL
  • UML---活动图
  • 编程笔记 Golang基础 018 常量与变量
  • 如何使用Douglas-042为威胁搜索和事件应急响应提速
  • 华为配置WLAN AC和AP之间VPN穿越示例
  • 跨语言的序列化与反序列化
  • 软考-中级-系统集成2023年综合知识(三)
  • 五、使用脚手架
  • 抛弃chatgpt,使用微软的Cursor提升coding效率
  • uniapp插件uViewplus的使用(涉及TS下的问题)
  • google浏览器chrome无法访问localhost等本地虚拟域名的解决方法
  • (2.2w字)前端单元测试之Jest详解篇