前言:
^ _ ^文件操作想必大家掌握的并不熟练,确实因为我们用的并不多,而本节内容能够让大家初步认识文件操作,从文件认识到文件使用,让我们对c语言文件操作有个初步的了解,在应用中可以看我用文件的形式完成一个通讯录
思维导图:
目录
一、文件操作:
1、 为什要使用文件操作:
2、什么是文件:
3、文件操作的使用:
3.1文件操作的打开和关闭:
3.2文件使用方式为:
3.3文件的顺序读写:
3.4文件的随机读写:
总结:
在c语言中我们完成一个程序后,他并不会对我们的数据进行保存,就像我上一篇文章中实现一个通讯录人员的增删插改中数据只是短暂的保存,而当程序退出后的同时人员信息也就销毁了,在此执行程序的时候人员信息并没有保存,而这是为什呢?是因为我们执行程序时我们写的代码信息数据不是持久化的,而要让他持久化就要用到文件操作,将我们写的数据存到文件中,而文件信息是存放到硬盘当中的,这样就达到了信息的持久化,也就在此使用时信息还是进行了一个保存。
2.1概念:
存放在磁盘的文件就是文件。文件分为两种:数据文件,程序文件。
2.1.1程序文件:
包括源程序文件(后缀为.c),目标文件(windows环境后缀为.obj),可执行程序(windows环境 后缀为.exe)。
2.1.2数据文件:
文件的内容不一定是程序,而是程序运行时读写的数据,比如程序运行需要从中读取数据的文件, 或者输出内容的文件。
2.2文件名:
一个文件要有一个唯一的文件标识,以便用户识别和引用。
文件名包含3部分:文件路径+文件名主干+文件后缀
例如: c:\code\test.txt 为了方便起见,文件标识常被称为文件名。
#include
#include
#include
int main()
{FILE*p=fopen("Contact.txt","w"); //打开文件输入文件地址,和打开方式if(p==NULL) //如果传入空指针则会报错{printf("%s", strerror(errno));}fclose(p); //关闭文件p=NULL; //防止变为野指针return 0;
}
3.3.1fputc:
#include
#include
#include
int main()
{FILE*p=fopen("Contact.txt","w");if(p==NULL){printf("%s", strerror(errno));}for(char i='a';i<'z';i++){fputc(i,p); //像文件中写入数据,写入的是单个字节}fclose(p);p=NULL;return 0;
}
3.3.2 fgetc:
#include
#include
#include
int main()
{FILE*p=fopen("Contact.txt","r");if(p==NULL){printf("%s", strerror(errno));}for(char i='a';i<'z';i++){printf("%c ",fgetc(p)); //从文件中获得数据打印出来}fclose(p);p=NULL;return 0;
}
3.3.3 fputs:
#include
#include
#include
int main()
{FILE*p=fopen("Contact.txt","w");//写入文件数据用w,读取数据用rif(p==NULL){printf("%s", strerror(errno));}fputs("hello world ",p); //向代码中写入字符串fclose(p);p=NULL;return 0;
}
3.3.4fgets:
#include
#include
#include
int main()
{FILE*p=fopen("Contact.txt","r");//写入文件数据用w,读取数据用rif(p==NULL){printf("%s", strerror(errno));}char arr[20]={0};fgets(arr,15,p);printf("%s\n",arr);fclose(p);p=NULL;return 0;
}
3.3.5fprintf:
#include
#include
#include
int main()
{FILE*p=fopen("Contact.txt","w");//写入文件数据用w,读取数据用rif(p==NULL){printf("%s", strerror(errno));}char arr[20]={"hello xiaoma"};fprintf(p,"%s",arr);fclose(p);p=NULL;return 0;
}
3.3.5fscanf:
#include
#include
#include
int main()
{FILE*p=fopen("Contact.txt","r");//写入文件数据用w,读取数据用rif(p==NULL){printf("%s", strerror(errno));}char arr[20]={0};fscanf(p,"%s",arr);printf("%s\n",arr);fclose(p);p=NULL;return 0;
}
3.3.6fwrite:
#include
#include
#include
int main()
{FILE*p=fopen("Contact.txt","wb");//写入文件数据用w,读取数据用r wb是以二进制的方式写,rb是以二进制的方式读if(p==NULL){printf("%s", strerror(errno));}char arr[20]={"hello world"};for(int i=0;i
3.3.7fread:
#include
#include
#include
int main()
{FILE*p=fopen("Contact.txt","rb");//写入文件数据用w,读取数据用r wb是以二进制的方式写,rb是以二进制的方式读if(p==NULL){printf("%s", strerror(errno));}char arr[20]={0};fread(arr,sizeof(arr),1,p);printf("%s\n",arr);fclose(p);p=NULL;return 0;
}
3.4.1fseek:
根据文件指针的位置和偏移量来定位文件指针
#include
#include
#include
int main()
{FILE*p=fopen("/Users/mamenghao/Desktop/Contact.txt","rb");//写入文件数据用w,读取数据用r wb是以二进制的方式写,rb是以二进制的方式读if(p==NULL){printf("%s", strerror(errno));}fseek(p,2,SEEK_SET); //SEEK_SET 偏移量从头开始// SEEK_CUR 偏移量从你上一次的位置开始// SEEK_END //偏移量从尾开始int ch =fgetc(p);printf("%c ",ch);fclose(p);p=NULL;return 0;
}
3.4.2ftell:
返回文件指针相对于起始位置的偏移量#include
#include
#include
int main()
{FILE*p=fopen("/Users/mamenghao/Desktop/Contact.txt","rb");//写入文件数据用w,读取数据用r wb是以二进制的方式写,rb是以二进制的方式读if(p==NULL){printf("%s", strerror(errno));}fseek(p,2,SEEK_SET); //SEEK_SET 偏移量从头开始// SEEK_CUR 偏移量从你上一次的位置开始// SEEK_END //偏移量从尾开始printf("%ld\n",ftell(p)); //这就打印了当前偏移量int ch =fgetc(p);printf("%c ",ch);fclose(p);p=NULL;return 0;
}
3.4.3rewind:
让文件指针的位置回到文件起始位置
#include
#include
#include
int main()
{FILE*p=fopen("/Users/mamenghao/Desktop/Contact.txt","rb");//写入文件数据用w,读取数据用r wb是以二进制的方式写,rb是以二进制的方式读if(p==NULL){printf("%s", strerror(errno));}fseek(p,2,SEEK_SET); //SEEK_SET 偏移量从头开始// SEEK_CUR 偏移量从你上一次的位置开始// SEEK_END //偏移量从尾开始printf("%ld\n",ftell(p)); //这就打印了当前偏移量rewind(p); //使偏移量回到了起始位置printf("%ld\n",ftell(p)); //在次打印偏移量的位置来确定是否回到起始位置int ch =fgetc(p);printf("%c ",ch);fclose(p);p=NULL;return 0;
}
总结:
对于文件操作并不是特别难,主要是我们对于文件操作不习惯的原因,让我们觉得文件操作很难,只要我么熟悉掌握fputc fgetc fputs fgets fprintf fscanf fwrite fread 这些函数的使用方法,文件操作也就容易理解啦,主要是在于操作,我们可以在下面把每一个都操作一下!!!
^ _ ^最后,孩子码文不易,如果觉得文章有帮助的话请多多支持呀!!!!😜