C语言_文件IO操作函数总结

网友投稿 596 2022-05-29

1. 文件IO总结

文件IO操作: 对文件系统里的文件进行: 打开、创建、读、写、关闭等运用。 C语言下标准文件IO接口(函数): (1)头文件: stdio.h 输入输出函数: printf 、scanf (2)相关函数: fopen、fread、fwrite、fclose 2.1 标准文件操作有两套函数: 1.标准C语言下的文件操作接口。fopen系列 常用于: 对普通文件的读写。 2.Linux操作系统下的文件操作接口。open系列 常用于: 对设备文件进行读写。 (鼠标、键盘、声卡、..)

2. C语言标准文件操作接口

2.1 最常用的4个函数

#include //打开文件 FILE *fopen(const char *path, const char *mode); //读文件 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); //写文件 size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream); //关闭文件 int fclose(FILE *fp);

2.3 写函数的基本运用

#include #include #include int main() { FILE *file; int cnt; /*1. 打开文件*/ file=fopen("D:/123.txt","a+b"); if(file==NULL) { printf("文件打开失败!\n"); return -1; } /*2. 写数据*/ cnt=fwrite("1234567890",1,10,file); /*3. 关闭文件*/ fclose(file); printf("cnt=%d\n",cnt); return 0; }

2.4 读函数基本运用

#include #include #include int main() { FILE *file; int cnt; char buff[100]; /*1. 打开文件*/ file=fopen("D:/123.txt","rb"); //malloc if(file==NULL) { printf("文件打开失败!\n"); return -1; } /*2. 写数据*/ cnt=fread(buff,1,100,file); /*3. 关闭文件*/ fclose(file); //free buff[cnt]='

#include #include #include int main() { FILE *file; int cnt; char buff[100]; /*1. 打开文件*/ file=fopen("D:/123.txt","rb"); //malloc if(file==NULL) { printf("文件打开失败!\n"); return -1; } /*2. 写数据*/ cnt=fread(buff,1,100,file); /*3. 关闭文件*/ fclose(file); //free buff[cnt]='\0'; printf("%s\n",buff); printf("cnt=%d\n",cnt); return 0; }

'; printf("%s\n",buff); printf("cnt=%d\n",cnt); return 0; }

2.5 文件指针位置偏移 (自动向后偏移)

#include #include #include int main() { FILE *file; int cnt; char data; /*1. 打开文件*/ file=fopen("D:/123.txt","rb"); //malloc if(file==NULL) { printf("文件打开失败!\n"); return -1; } /*2. 读数据---验证文件指针是否可否自动向后偏移*/ cnt=fread(&data,1,1,file); printf("data=%c\n",data); cnt=fread(&data,1,1,file); printf("data=%c\n",data); cnt=fread(&data,1,1,file); printf("data=%c\n",data); cnt=fread(&data,1,1,file); printf("data=%c\n",data); cnt=fread(&data,1,1,file); printf("data=%c\n",data); /*3. 关闭文件*/ fclose(file); //free return 0; }

2.6 设置文件指针位置

C语言_文件IO操作函数总结

#include #include #include int main() { FILE *file; int cnt; char data; /*1. 打开文件*/ file=fopen("D:/123.txt","rb"); //malloc if(file==NULL) { printf("文件打开失败!\n"); return -1; } /*2. 偏移文件指针*/ fseek(file,5,SEEK_SET); /*3. 读数据---验证文件指针是否可否自动向后偏移*/ cnt=fread(&data,1,1,file); printf("data=%c\n",data); /*4. 关闭文件*/ fclose(file); //free return 0; }

2.7 以上午所学的函数,如何判断文件读完了?到文件结尾?

#include #include #include int main() { FILE *file; int cnt; char data; /*1. 打开文件*/ file=fopen("D:/123.txt","rb"); //malloc if(file==NULL) { printf("文件打开失败!\n"); return -1; } /*2. 偏移文件指针*/ fseek(file,5,SEEK_SET); /*3. 读数据---验证文件指针是否可否自动向后偏移*/ while(1) { cnt=fread(&data,1,1,file); if(cnt!=1)break; printf("data=%c\n",data); } /*4. 关闭文件*/ fclose(file); //free return 0; }

2.8 文件读写结构体数据

//写结构体数据 #include #include #include struct MyStruct { int a; int b; char c[100]; }; int main() { FILE *file; int cnt; struct MyStruct stu={666,888,"C语言文件操作学习"}; /*1. 打开文件*/ file=fopen("D:/123.txt","wb"); if(file==NULL) { printf("文件打开失败!\n"); return -1; } /*2. 读数据*/ cnt=fwrite(&stu,1,sizeof(struct MyStruct),file); printf("cnt=%d\n",cnt); /*3. 关闭文件*/ fclose(file); //free return 0; } //读结构体数据 #include #include #include struct MyStruct { int a; int b; char c[100]; }; int main() { FILE *file; int cnt; struct MyStruct stu; /*1. 打开文件*/ file=fopen("D:/123.txt","rb"); if(file==NULL) { printf("文件打开失败!\n"); return -1; } /*2. 读数据*/ cnt=fread(&stu,1,sizeof(struct MyStruct),file); printf("cnt=%d\n",cnt); printf("%d,%d,%s\n",stu.a,stu.b,stu.c); /*3. 关闭文件*/ fclose(file); //free return 0; }

2.9 文件操作的作业练习

1. 学习文件基本读写使用 2. 编写文件拷贝程序。 实现文件拷贝。 3. 文件加密解密实现。 需要编写一个菜单。 4. 完善学生管理系统。 需要将所有学生信息保存到文件里,完善功能。

C 语言

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:C程序的内存空间分配
下一篇:Google Earth Engine——gee文件导出到drive:影像等大文件导出100000000超限解决办法
相关文章