目录
- 前言
- 一、核心概念:
- 二、关键操作步骤:
- 三、为什么需要文件IO?
- 四、常见类型:
- 五、标准IO源码
- 六、笔试真题和练习
- 1.
- 代码实现1
- 代码实现2
- 2.
- 代码实现
- 3.
- 代码实现
- 4.
- 代码实现
- 5.
- 代码实现
- 七、总结
前言
文件IO(文件输入/输出) 是指计算机程序与外部存储设备(如硬盘、SSD、U盘等)上的文件进行数据交换的过程。简单来说,就是程序从文件中读取数据(Input)或将数据写入文件(Output)。文件IO这个板块我们会学习很多个接口,所以说我们长时间不用就会忘记,一定要会用man指令查手册,必须要回看得懂源码。
一、核心概念:
文件(File):
存储在持久化介质(如硬盘)上的数据集合,具有名称和路径。可以是文本、图片、音频、程序等任何形式。
输入(Input):
程序从文件读取数据到内存(如变量、数据结构)。例如:读取配置文件、加载图片数据。
输出(Output):
程序将内存中的数据写入文件。例如:保存用户设置、导出计算结果。
二、关键操作步骤:
打开文件(Open):
建立程序与文件的连接,指定操作模式(读、写、追加等)。
读写数据(Read/Write):
**读:**将文件内容传输到程序内存。
**写:**将程序内存中的数据写入文件。
关闭文件(Close):
释放资源,确保数据完整保存(重要!未关闭可能导致数据丢失)。
三、为什么需要文件IO?
**持久化存储:**内存数据在程序关闭后消失,文件可长期保存。
**数据共享:**不同程序或用户可通过文件交换数据。
**处理大数据:**内存有限,文件可分批读取/写入海量数据。
四、常见类型:
**文本文件IO:**处理字符数据(如 .txt, .csv),需注意编码(UTF-8等)。
**二进制文件IO:**直接处理字节(如图片 .jpg、可执行程序),无编码转换。
五、标准IO源码
因为标准IO的接口都是在Linux的man手册的第3长,在Linux系统输入man 3 fopen,man 3 fread,等常用的函数接口
六、笔试真题和练习
1.
代码实现1
这次我用的是fopen打开文件,fclose关闭文件,fgetc一个一个字符读取文件中的内容,所以光标会自己向后偏移。
#include <stdio.h>
#include <stdlib.h>int main(int argc, char const *argv[])
{if(argc != 2){printf("argument is invailed!\n");exit(1);}FILE* file = fopen(argv[1], "rb");if(!file){printf("file open failed!\n");exit(1);}int cnt = 0;while(1){if(fgetc(file) == EOF){break;}cnt++;}printf("file %s size is %d byte\n",argv[1],cnt);fclose(file);return 0;
}
代码实现2
这个代码是利用fseek函数将文件的光标移动到文件末尾,然后用ftell函数记录当前光标位置到文件开头的偏移值。
#include <stdio.h>
#include <stdlib.h>int main(int argc, char const *argv[])
{if(argc != 2){printf("argument is invailed!\n");exit(1);}FILE* file = fopen(argv[1], "rb");if(!file){printf("file open failed!\n");exit(1);}fseek(file,0,SEEK_END);printf("file %s size is %ld byte\n",argv[1], ftell(file));fclose(file);return 0;
}
2.
### 代码实现
用到了fread和fwrite接口,定义一个缓冲区(buffer),记录读写次数,每次读写前把缓冲区清空。
代码实现
/** Copyright (c)* * date: 2025-7-22* * author: Charles* * function name : copyFile** function: 把一个文件的所有内容拷贝到另一个文件*/#include <stdio.h>
#include <stdlib.h>
#include <strings.h>#define BUFFERSIZE 512int main(int argc, char const *argv[])
{long file_src_size = 0; //源文件的总字节数long remainder = 0; //总字节数与缓冲区字节数的余数int cnt = 0; //用源文件总字节数除以缓冲区字节数,也就是再循环里的读写次数long file_des_size = 0; //目标文件的总字节数char buffer[BUFFERSIZE] = {0}; //缓冲区if(argc != 3){printf("argument is invailed!\n");exit(1);}FILE* file_src = fopen(argv[1], "rb");FILE* file_des = fopen(argv[2], "wb");if(!file_src || !file_des){printf("file open failed!\n");exit(1);}fseek(file_src, 0, SEEK_END);file_src_size = ftell(file_src);fseek(file_src, 0, SEEK_SET);printf("file_src [%s] size is [%ld] byte\n",argv[1], file_src_size);cnt = file_src_size / BUFFERSIZE;remainder = file_src_size % BUFFERSIZE;while(cnt--){memset(buffer, 0, BUFFERSIZE);size_t n = fread(buffer, 1, BUFFERSIZE, file_src);fwrite(buffer, 1, n, file_des);}if(remainder){memset(buffer, 0, BUFFERSIZE);size_t n = fread(buffer, 1, remainder, file_src);fwrite(buffer, 1, n, file_des);}fseek(file_des, 0, SEEK_END);file_des_size = ftell(file_des);fseek(file_des, 0, SEEK_SET);printf("file_des [%s] size is [%ld] byte\n",argv[2], file_des_size);fclose(file_src);fclose(file_des);return 0;
}
3.
代码实现
在一个循环里用open函数,记录直到函数返回值为-1为止。
/** Copyright (c)* * date: 2025-7-22* * author: Charles* * function name : FileOpenCount** function: 测试文件打开次数*/#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#define BUFFERSIZE 512int main(int argc, char const *argv[])
{if(argv != 2){printf("argument is invailed!\n");exit(1);}int cnt = 0;while(open(argv[1], O_RDWR) != -1){cnt++;}printf("file can be open %d count!\n",cnt + 3);//为什么加3,因为在打开这个文件的时候系统会自动打开标准输入,标准输出,错误这个文件return 0;
}
4.
像这个获取时间的api,都是系统IO。
代码实现
/** Copyright (c)* * date: 2025-7-23* * author: Charles* * function name : time_log.c** function: 每秒钟打印当前日期到log.txt文件中*/#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>int main(int argc, char const *argv[])
{while(1){time_t t = time(NULL);struct tm *tm_info = localtime(&t);char day[8][20] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};char buffer[256];snprintf(buffer,sizeof(buffer),"%d年%02d月%02d日 %s %02d:%02d:%02d",tm_info->tm_year + 1900,tm_info->tm_mon + 1,tm_info->tm_mday,day[tm_info->tm_wday],tm_info->tm_hour,tm_info->tm_min,tm_info->tm_sec);FILE* file = fopen("./log.txt","a");if(!file){perror("fopen");exit(-1);}fputs(buffer, file);fclose(file);sleep(1);}return 0;
}
5.
这题可以看看bmp的文件存储格式图。然后要注意的是系统默认会有一个对齐机制,就比如说bmp_header这个结构体如果取消对齐大小为14字节,如果不取消大小就为16字节。
代码实现
/** Copyright (c)* * date: 2025-7-23* * author: Charles* * function name : getBMP.c** function: 获取bmp图片大小,像素宽度和高度信息*/#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>#pragma pack(1) //取消字节对齐struct bmp_header{char s[2];int size;short a;short b;int c;
};struct bmp_info{int a;int width;int heigth;short a1;short b1;int off1;int off2;int off3;int off4;int off5;int off6;
};#pragma pack(0)int main(int argc, char const *argv[])
{// int bmp_size = 0; //bmp图片的总字节数// int bmp_width = 0; //bmp图片的像素宽度// int bmp_heigth = 0; //bmp图片的像素高度if(argc != 2){ //参数错误处理printf("argument fail! \n");exit(-1);}int bmp_fd = open(argv[1], O_RDWR);if(bmp_fd == -1){printf("open file : %s fail\n", argv[1]);exit(-1);}// char header[2] = {0};// read(bmp_fd, header, 2);struct bmp_header bh;struct bmp_info bi;read(bmp_fd, &bh, 14);read(bmp_fd, &bi, 40);if(bh.s[0] != 'B' || bh.s[1] != 'M'){ //判断写入的两个字符是否是BM开头的printf("open file not bmp\n");exit(-1);}printf("bmp size is %d byte\n", bh.size);printf("bmp width is %d px\n", bi.width);printf("bmp heigth is %d py\n", bi.heigth);// lseek(bmp_fd, 2, SEEK_SET);// read(bmp_fd, &bmp_size, 4);// printf("bmp size is %d byte\n", bmp_size);// lseek(bmp_fd, 18, SEEK_SET);// read(bmp_fd, &bmp_width, 4);// printf("bmp width is %d byte\n", bmp_width);// lseek(bmp_fd, 18 + 4, SEEK_SET);// read(bmp_fd, &bmp_heigth, 4);// printf("bmp heigth is %d byte\n", bmp_heigth);close(bmp_fd);return 0;
}
七、总结
文件IO是程序与外部存储交互的桥梁,通过 读取(Input) 和 写入(Output) 实现数据的持久化和重用,是几乎所有软件的基础功能。不同编程语言有各自的API(如C的 fopen/fread、Java的 FileInputStream、Python的 open()),但核心逻辑一致。
希望各位靓仔靓女点赞,收藏,关注多多支持,我们共同进步,后续我会更新更多的面试真题,你们的支持将是我前进最大的动力。