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

【Linux】基础IO(1)

1. 理解“文件”

1.1 狭义理解

文件在磁盘里
磁盘是永久性存储介质,因此⽂件在磁盘上的存储是永久性的
磁盘是外设(即是输出设备也是输⼊设备)
磁盘上的⽂件 本质是对⽂件的所有操作,都是对外设的输⼊和输出 简称 IO

1.2 广义理解

Linux 下⼀切皆⽂件(键盘、显⽰器、⽹卡、磁盘…… 这些都是抽象化的过程)

1.3 文件操作的归类认知

对于 0KB 的空⽂件是占⽤磁盘空间的
⽂件是⽂件属性(元数据)和⽂件内容的集合(⽂件 = 属性(元数据)+ 内容)
所有的⽂件操作本质是⽂件内容操作和⽂件属性操作

1.4 系统角度

访问文件需先执行打开操作,而文件的打开由进程发起,因此对文件的所有操作,本质上都是进程对文件的操作。
磁盘的管理者是操作系统
⽂件的读写本质不是通过 C 语⾔ / C++ 的库函数来操作的(这些库函数只是为⽤户提供⽅便),而是通过⽂件相关的系统调⽤接⼝来实现的

2. 回顾C文件接口文件

2.1 打开文件

 #include<stdio.h>int main(){FILE* fp = fopen("newfile", "w");if(!fp) printf("fopen error\n");fclose(fp);                                                                                                                     return 0;}
  • fopen函数用于打开文件,第一个参数是文件名,第二个参数是打开模式
  • 打开模式 "w" 表示以写入方式打开,如果文件不存在则创建,如果存在则清空原有内容
  • 函数返回FILE*类型的文件指针,作为后续文件操作的句柄
  • 必须检查返回值,如果为NULL表示打开失败(可能是权限问题等)

2.2 写文件

#include<stdio.h>
#include<string.h>int main()
{FILE* fp = fopen("newfile", "w");if(!fp) printf("fopen error\n");const char* msg = "hello Linux!\n";int count = 5;while(count--){fwrite(msg, strlen(msg), 1, fp);}fclose(fp);return 0;
}
  • fwrite函数原型:size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
  • 这里表示每次写入 1 个大小为strlen(msg)的元素(即整个字符串)
  • 循环 5 次,所以会将 "hello Linux!\n" 写入文件 5 次

2.3 读文件

#include <stdio.h>
#include <string.h>int main()
{FILE* fp = fopen("newfile", "r");if(!fp){printf("fopen error!\n");return 1;}char buf[1024];const char* msg = "hello Linux!\n";while(1){size_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;
}
  • fread函数原型:size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
  • 这里每次读取strlen(msg)个大小为 1 的元素(即与写入的字符串长度相同)
  • feof(fp)用于检查是否到达文件末尾
  • 读取到内容后手动添加字符串结束符\0,因为fread不会自动添加

    稍作修改,简单实现cat命令

    #include <stdio.h>
    #include <string.h>
    int main(int argc, char* argv[])
    {if (argc != 2){printf("argv error!\n");return 1;}FILE* fp = fopen(argv[1], "r");if (!fp) {printf("fopen error!\n");return 2;}char buf[1024];while (1) {int s = fread(buf, 1, sizeof(buf), fp);if (s > 0) {buf[s] = 0;printf("%s", buf);}if (feof(fp)) {break;}}fclose(fp);return 0;
    }

    2.4 输出信息到显示器,你有哪些方法

    #include <stdio.h>
    #include <string.h>
    int main()
    {const char* msg = "hello fwrite\n";fwrite(msg, strlen(msg), 1, stdout);printf("hello printf\n");fprintf(stdout, "hello fprintf\n");return 0;
    }

    2.5 stdin & stdout & stderr

    C默认会打开三个输⼊输出流,分别是stdin, stdout, stderr
    • stdin:标准输入(键盘),行缓冲
    • stdout:标准输出(屏幕),行缓冲(遇\n刷新)
    • stderr:标准错误(屏幕),无缓冲(立即输出)
    仔细观察发现,这三个流的类型都是FILE*, fopen返回值类型,⽂件指针
    #include <stdio.h>
    extern FILE *stdin;
    extern FILE *stdout;
    extern FILE *stderr;

    2.6 打开文件的方式

    • r Open text file for reading.The stream is positioned at the beginning of the file.

    • r+ Open for reading and writing.The stream is positioned at the beginning of the file.

    • w Truncate file to zero length or create text file for writing.The stream is positioned at the beginning of the file.

    • w+ Open for reading and writing.The file is created if it does not exist, otherwise it is truncated.The stream is positioned at the beginning of the file.

    • a Open for appending (writing at end of file).The file is created if it does not exist.The stream is positioned at the end of the file.

    • a+ Open for reading and appending (writing at end of file).The file is created if it does not exist.The initial file position for reading is at the beginning of the file,but output is always appended to the end of the file.

    • r:打开文本文件用于读取。流定位在文件开头(文件必须存在)。
    • r+:打开文本文件用于读写。流定位在文件开头(文件必须存在)。
    • w:创建文本文件用于写入,若文件已存在则截断为零长度。流定位在文件开头。
    • w+:打开文本文件用于读写,若文件不存在则创建,若已存在则截断。流定位在文件开头。
    • a:打开文件用于追加(在文件末尾写入),若文件不存在则创建。流定位在文件末尾。
    • a+:打开文件用于读取和追加,若文件不存在则创建。读取的初始位置在文件开头,但输出始终追加到文件末尾。
    http://www.dtcms.com/a/399998.html

    相关文章:

  • 在目标图像中查找带 Alpha 掩码的 PNG 图标
  • MQTT 镜像部署文档
  • IPv4 报文中标识字段的理解
  • C语言:实现有序数组插入元素
  • day02-电路基础2
  • 开发避坑指南(56):IDEA2025.1.3新版本,这样配置工具栏,常用操作速度提升50%
  • springboot项目添加请求链路追踪日志traceId
  • 网站建化网站开发外包业务怎么接
  • 网页源代码 企业网站源码 html源码网站
  • IGBT反压限制
  • 做团购网站视频石家庄网络营销
  • 南宁本地有几家网站开发网站建设技术人员要会什么
  • 项目实战:RAG论文阅读助理系统性能测试
  • 【生态系统植被净初级生产力NPP】CASA(Carnegie-Ames-Stanford Approach)模型原理及应用
  • 济南门户网站建设网站建设菜单栏设计
  • 文昌品牌网站建设费用域名关键词查询
  • 整体设计 逻辑全链 之7 受控的自然语言-字面拼凑:正则表达式 之1
  • ABC424A-E题解
  • 基于深度学习的双对数坐标曲线转折点识别方法研究
  • 部门网站建设管理制度网站开发部署
  • 孟庆涛:GEO 三大趋势工具到生态,构建生成式 AI 时代的认知主权
  • 如何建一个公司网站WordPress不显示斜杠
  • 家政公司网站建设方案网站建设捌金手指下拉六
  • 北京超市网站建设孝感的网站建设
  • 中国精品课程网站湖南省郴州市有几个县
  • 非参数方法:数据驱动时代 “无分布约束” 的分析利器 —— 技术实践与方法论升华
  • Python typing库的应用与优缺点
  • STM32与7038芯片通过SPI通信读取寄存器数据
  • 跨部门设计评审不足常见的问题有哪些
  • PyTorch 模型构建