文件的打开
1.为什么使用文件
我们前面学习结构体的时候时,写了通讯录的程序,当通讯录运行起来的时候,可以给通讯录中增加,删除数据,此时数据是存放在内存中,当程序退出的时候,通讯录中的数据自然就不存在了,等下次运行通讯录程序的时候,数据又得重新录入,如果使用这样的通讯录就很难受
我们在想既然是通讯录就应该把信息记录下来,只有我们自己选择删除数据的时候,数据才不复存在,这就涉及了数据持久化的问题,我们一般数据持久化的方法有,把数据存放在磁盘文件,存放到数据库等方式
使用文件我们可以将数据直接存放在电脑的硬盘上,做到了数据的持久化。
2.文件的分类
2.1程序文件
包括源程序文件(后缀为.c),目标文件(windows环境后缀为.obj),可执行程序(windows环境后缀为.exe)。
2.2数据文件
文件的内容不一定是程序,而是程序运行时读写的数据,比如程序运行需要从中读取数据的文件,或者输出内容的文件。
2.3文件名
一个文件要有一个唯一的文件标识(即文件名),以便用户识别和引用
包含3部分:文件路径+文件名主干+文件后缀
例如:c:\code\ test .txt
3.文件的打开和关闭
3.1文件指针
缓冲文件系统中,关键的概念是“文件类型指针”,简称“文件指针”。
每个被使用的文件都在内存中开辟了一个相异的文件信息区,用来存放文件的相关信息(如文件的名字,文件状态以及文件当前的位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是由系统声明的,取名FILE.
每当打开一个文件的时候,系统会根据文件的情况自动创建一个FILE结构的变量,并填充其中的信息(文件信息区)
一般是通过一个FILE的指针来维护这个FILE结构的变量,
下面我们可以创建一个FILE*的指针变量
FILE* pf;//文件指针变量
定义pf是一个指向FILE类型数据的指针变量,可以使pf指向某个文件的文件休息区(是一个结构体变量)。通过该文件信息区中的信息就能够访问该文件,也就是说,通过文件指针变量能够找到与它关联的文件
文件在读写之前应该先打开文件,在使用结束之后应该关闭文件
在编写程序的时候,在打开文件的同时,都会返回一个FILE*的指针变量指向该文件,也相当于建立了指针和文件的关系
ANSIC规定使用fopen函数来打开文件,fclose来关闭文件。
#include<string.h>
#include<errno.h>
int main()
{
FILE* pf=fopen("test.txt","r");//相对路径
FILE* pf=fopen("C:\\Users\\zpeng\\Desktop\\test.txt","r")//绝对路径
if(pf==NULL)
{
printf("%s\n",strerror(errno));
return 1;
}
//...
//读文件
//关闭文件
fclose(pf);
pf=NULL;
return 0;
}
4.文件的顺序读写
4.1 fputc
int fputc(int character,FILE*stream)
写一个字符:
#include<stdio.h>
#include<errno.h>
int main()
{
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
//写文件
char i = 0;
for (i = 'a';i <= 'z';i++)
{
fputc(i, pf);
}
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4.2 fgetc
int fgetc(FILE* stream)
一个一个读,读取失败会遇到"EOF"
读一个字符:
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
//读文件
////读文件
//int ch = fgetc(pf);
//printf("%c\n", ch);
// ch = fgetc(pf);
//printf("%c\n", ch);
//ch = fgetc(pf);
//printf("%c\n", ch);
//ch = fgetc(pf);
//printf("%c\n", ch);
int ch = 0;
while ((ch = fgetc(pf)) != EOF)
{
printf("%c ", ch);
}
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4.3 fputs
int fputs(const char* str,FILE* stream);
写一行数据:
int main()
{
FILE* pf = fopen("test.txt", "w");//如果文件里面由内容,会将原来的内容销毁重新写入新内容,不想销毁的话,使用"a"即追加
//"w"在打开文件的时候就清理了文件里面的内容
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
//写一行数据
fputs("hello bit", pf);
fputs("hello bit", pf);//这样的结果是在同一行
//想在两行的话:
fputs("hello bit\n", pf);
fputs("hello bit\n", pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4.4 fgets
char* fgets(char* str,int num,FILE* stream);
读一行数据:
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
//perror("fopen");
return 1;
}
//读一行数据
char arr[20];
fgets(arr,5,pf);
printf("%s\n", arr);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
perror
打印错误信息
void perror(const char* str);
4.5 fprintf
int fprintf(FILE * stream,const char* format,...);
struct S
{
char arr[10];
int age;
float score;
};
int main()
{
struct S s = { "zhangsan",25,50.5f };
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
fprintf(pf, "%s %d %f", s.arr, s.age, s.score);
fclose(pf);
pf = NULL;
return 0;
}
4.6 fscanf
int fscanf(FILE * stream,const char* format,...);
struct S
{
char arr[10];
int age;
float score;
};
int main()
{
struct S s = {0};
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
fscanf(pf, "%s %d %f", s.arr,&( s.age), &(s.score));
printf("%s %d %f\n", s.arr, s.age, s.score);
//fprintf(stdout,"%s %d %f\n",s.arr,s.age,s.score);
fclose(pf);
pf = NULL;
return 0;
}
流(输入输出缓冲区)
FILE*
任何一个c程序,只要运行起来就会默认打开3个流:
1.FILE* stdin-标准输入流(键盘)[scanf]
2.FILE* stdout-标准输出流(屏幕)[printf]
3.FILE* stderr-标准错误流(屏幕)
4.7 fwrite
size_t fwrite(const void* ptr,size_t size,size_t count,FILE*stream);
struct S
{
char arr[10];
int age;
float score;
};
int main()
{
struct S s = { "zhangsan",25,50.5f };
//以二进制的形式写到文件中
FILE* pf = fopen("text.txt", "wb");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//二进制的方式写
fwrite(&s, sizeof(struct S), 1, pf);
fclose(pf);
pf = NULL;
return 0;
}
4.7 fread
size_t fread(void* ptr,size_t size,size_tcount,FILE* stream);
struct S
{
char arr[10];
int age;
float score;
};
int main()
{
struct S s = { "zhangsan",25,50.5f };
//以二进制的形式写到文件中
FILE* pf = fopen("text.txt", "wb");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//二进制的方式写
fread(&s, sizeof(struct S), 1, pf);
printf("%s %d %f\n", s.arr, s.age, s.score);
fclose(pf);
pf = NULL;
return 0;
}
5.对比一组函数
5.1 scanf/fscanf/sscanf
scanf 是针对标准输入的格式化输入语句
fscanf 是针对所以输入流的格式化输入语句
sscanf 从一个字符串中转化出一个格式化的数据
sscanf:
int sscanf(const char* s,const char* format,...);
5.2 printf/fprintf/sprintf
printf 是针对标准输出的格式化输出语句
fscanf 是针对所有输入流的格式化输入语句
sprintf 把s中的格式化数据转化成字符串
sprintf:
int sprintf(const char* str,const char* format,...);
把一个格式化的数据写到字符串中,本质是把一个格式化的数据转换成字符串
sscanf和sprintf的栗子:
struct S
{
char arr[10];
int age;
float score;
};
int main()
{
struct S s = { "zhangsan",20,55.5f };
struct S tmp = { 0 };
char buf[100] = { 0 };
//把s中的格式化数据转化成字符串放到buf中
sprintf(buf, "%s %d %f", s.arr, s.age, s.score);
printf("字符串:%s\n", buf);
//从字符串buf中获取一个格式化的数据1到tmp中
sscanf(buf, "%s %d %f", tmp.arr, &(tmp.age), &(tmp.score));
printf("格式化:%s %d %f\n", tmp.arr, tmp.age, tmp.score);
return 0;
}
6.文件的随机读写
6.1 fseek
根据文件指针的位置和偏移量来定位文件指针
int fseek(FILE * stream,long int offset,int origin);
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
////读文件
//定位文件指针
fseek(pf,2,SEEK_SET);
int ch = fgetc(pf);//c
printf("%c\n", ch);
fseek(pf, 2, SEEK_CUR);
//fseek(pf,-1,SEEK_END);
ch = fgetc(pf);
printf("%c\n", ch);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
6.2 fteel
返回文件指针相对于起始位置的偏移量
lonng int ftell(FILE * stream);
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
////读文件
//定位文件指针
fseek(pf,2,SEEK_SET);
int ch = fgetc(pf);//c
printf("%c\n", ch);
printf("%d\n", ftell(pf));//3
//fseek(pf, 2, SEEK_CUR);
fseek(pf,-1,SEEK_END);
ch = fgetc(pf);
printf("%c\n", ch);
printf("%d\n", ftell(pf));
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
6.3 rewind
让文件指针的位置回到文件的起始位置
void rewind(FILE * stream);
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
////读文件
//定位文件指针
fseek(pf,2,SEEK_SET);
int ch = fgetc(pf);//c
printf("%c\n", ch);
printf("%d\n", ftell(pf));//3
//fseek(pf, 2, SEEK_CUR);
fseek(pf,-1,SEEK_END);
ch = fgetc(pf);
printf("%c\n", ch);
printf("%d\n", ftell(pf));
rewind(pf);
ch = fgetc(pf);
printf("%c\n", ch);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}