news 2026/9/12 15:13:47

Linux基础IO

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Linux基础IO

1:C语言文件IO

C语言中的文件操作函数如下:

文件操作函数

功能

fopen

打开文件

fclose

关闭文件

fputc

写入一个字符

fgetc

读取一个字符

fputs

写入一个字符串

fgets

读取一个字符串

fprintf

格式化写入数据

fscanf

格式化读取数据

fwrite

向二进制文件写入数据

fread

从二进制文件读取数据

fseek

设置文件指针的位置

ftell

计算当前文件指针相对于起始位置的偏移量

rewind

设置文件指针到文件的起始位置

ferror

判断文件操作过程中是否发生错误

feof

判断文件指针是否读取到文件末尾

下面只会选择性对C语言的部分文件操作函数进行使用,若想详细了解其余文件操作函数的使用方法,请跳转到博主的其它博客:文件处理不再难:带你轻松攻克C语言文件操作_c语言大文件处理-CSDN博客

代码语言:javascript

AI代码解释

#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <stdlib.h> int main() { FILE * fp = fopen("log.txt","w"); if(NULL == fp) { perror("fopen"); exit(-1); } fclose(fp); fp = NULL; return 0; }

运行程序后,在当前路径下就会生成对应文件.

那么我们也可以通过输出重定向向文件里面写入新的内容.

1.1:对文件的初步理解

那么有了上面的简单例子我们可以对文件有一个简单的初步理解

  • 打开文件,本质是进程在打开文件.
  • 文件在没有被打开的时候,在什么地方上------->在磁盘上头.
  • 进程能够同时打开很多文件.

系统中可以存在很多进程------------>很多情况下,OS内部,一定存在大量的被打开的文件---------->那么OS就要对这些被打开的文件进行处理,先描述,再组织------->因此可以推测,每一个被打开的文件在OS内部,一定要存在对应的描述文件属性的结构体,类似与PCB.


www.dongchedi.com/article/7594897166766965272
www.dongchedi.com/article/7594897310665245246
www.dongchedi.com/article/7594897192310407705
www.dongchedi.com/article/7594897631617794585
www.dongchedi.com/article/7594896580080435737
www.dongchedi.com/article/7594895704896684568
www.dongchedi.com/article/7594897374330503705
www.dongchedi.com/article/7594895618720449048
www.dongchedi.com/article/7594896034183397912
www.dongchedi.com/article/7594896087656694334
www.dongchedi.com/article/7595300698343260734
www.dongchedi.com/article/7595300086620734014
www.dongchedi.com/article/7595300794661257753
www.dongchedi.com/article/7595300922285146649
www.dongchedi.com/article/7595300794661159449
www.dongchedi.com/article/7595301535278858814
www.dongchedi.com/article/7595300053246837310
www.dongchedi.com/article/7595298997771141656
www.dongchedi.com/article/7595298747215987224
www.dongchedi.com/article/7595298653444080190
www.dongchedi.com/article/7595299173835276862
www.dongchedi.com/article/7595293834620191257
www.dongchedi.com/article/7595294214674596377
www.dongchedi.com/article/7595294735703966270
www.dongchedi.com/article/7595294214674825753
www.dongchedi.com/article/7595269939515703870
www.dongchedi.com/article/7595270106293600793
www.dongchedi.com/article/7595268901785584153
www.dongchedi.com/article/7595268784974545433
www.dongchedi.com/article/7595270592459506238
www.dongchedi.com/article/7595268816629219865
www.dongchedi.com/article/7595267640395235902
www.dongchedi.com/article/7595267822872166937
www.dongchedi.com/article/7595266120056422936
www.dongchedi.com/article/7595265326464795161
www.dongchedi.com/article/7595264479055970878
www.dongchedi.com/article/7595263814942376472
www.dongchedi.com/article/7595263889965924926
www.dongchedi.com/article/7595264219466121753
www.dongchedi.com/article/7595261441830486590
www.dongchedi.com/article/7595257155528409624
www.dongchedi.com/article/7595257148431974937
www.dongchedi.com/article/7595255577883198014
www.dongchedi.com/article/7595257264999858713
www.dongchedi.com/article/7595255131160085054
www.dongchedi.com/article/7595254038942401086
www.dongchedi.com/article/7595272535034430014
www.dongchedi.com/article/7595273235844137496
www.dongchedi.com/article/7595271162356302398
www.dongchedi.com/article/7595269071357231641
www.dongchedi.com/article/7595269220477567512
www.dongchedi.com/article/7595268287076958782
www.dongchedi.com/article/7595269071357264409
www.dongchedi.com/article/7595268315833270809
www.dongchedi.com/article/7595265492903363096
www.dongchedi.com/article/7595265055567299096
www.dongchedi.com/article/7595265773019841049
www.dongchedi.com/article/7595265891500114494
www.dongchedi.com/article/7595264904798978585
www.dongchedi.com/article/7595263429863195161
www.dongchedi.com/article/7595261995696800280
www.dongchedi.com/article/7595258440394818073
www.dongchedi.com/article/7595256795065893401
www.dongchedi.com/article/7595256961541767704
www.dongchedi.com/article/7595257257731408446
www.dongchedi.com/article/7595253896583774745
www.dongchedi.com/article/7595252944199778841

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/9 21:59:02

从零开始学Linux进程控制:fork、wait、exec 详解

2:创建子进程会经过以下步骤.分配新的内存块和内核数据结构给子进程.将父进程部分数据结构内容拷贝给子进程(子进程要继承于父进程).添加子进程到系统的进程列表中代码:子进程与父进程共享代码数据:则通过写时拷贝的方式如果理解进程具有独立性根本原因在于:进程 内核的相关管…

作者头像 李华
网站建设 2026/9/9 21:58:26

Qwen All-in-One Docker部署:容器化实践指南

Qwen All-in-One Docker部署&#xff1a;容器化实践指南 1. 引言 1.1 业务场景描述 在边缘计算和资源受限的生产环境中&#xff0c;AI服务的轻量化与高效部署成为关键挑战。传统方案通常采用多个专用模型&#xff08;如BERT用于情感分析、LLM用于对话&#xff09;并行运行&a…

作者头像 李华
网站建设 2026/9/9 22:46:33

3步彻底解决RTX 5070显卡风扇异常问题

3步彻底解决RTX 5070显卡风扇异常问题 【免费下载链接】FanControl.Releases This is the release repository for Fan Control, a highly customizable fan controlling software for Windows. 项目地址: https://gitcode.com/GitHub_Trending/fa/FanControl.Releases …

作者头像 李华
网站建设 2026/9/9 22:46:09

网易云音乐全能助手:解锁云盘快传与无损下载新体验

网易云音乐全能助手&#xff1a;解锁云盘快传与无损下载新体验 【免费下载链接】myuserscripts 油猴脚本:网易云音乐:云盘歌曲快传(含周杰伦),歌曲下载,转存云盘,云盘匹配纠正,听歌量打卡,本地上传云盘 咪咕音乐:歌曲下载 项目地址: https://gitcode.com/gh_mirrors/my/myuse…

作者头像 李华
网站建设 2026/9/11 19:57:50

PaddleOCR弯曲文本识别:云端GPU1小时解决古籍难题

PaddleOCR弯曲文本识别&#xff1a;云端GPU1小时解决古籍难题 你是不是也遇到过这样的情况&#xff1a;手里一堆珍贵的竹简、古籍或碑拓&#xff0c;上面的文字因为年代久远而严重弯曲、扭曲甚至断裂&#xff0c;传统的OCR工具一打开就“罢工”——要么识别不了&#xff0c;要…

作者头像 李华
网站建设 2026/9/9 22:46:07

Tablacus Explorer:重新定义Windows文件管理的多标签神器

Tablacus Explorer&#xff1a;重新定义Windows文件管理的多标签神器 【免费下载链接】TablacusExplorer A tabbed file manager with Add-on support 项目地址: https://gitcode.com/gh_mirrors/ta/TablacusExplorer 还在为Windows资源管理器的单一窗口而烦恼吗&#x…

作者头像 李华