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* pf;//文件指针变量
定义pf是一个指向FILE类型数据的指针变量。可以使pf指向某个文件的文件信息区(是一个结构体变量)。通过该文件信息区中的信息就能够访问该文件。也就是说,通过文件指针变量能够找到与它关联的文件
3.2文件的打开和关闭
文件在读写之前应该先打开文件,在使用结束之后应该关闭文件。
FILE * fopen ( const char * filename, const char * mode );
int fclose ( FILE * stream );
int main()
{
//打开文件
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
perror("fopen");
return -1;
}
//读写文件
//.......
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4.文件的顺序读写
//C语言程序默认打开三个流,标准输出流 stdout,标准输入流 stdin,标准错误流 stderr
int main()
{
//打开文件
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
perror("fopen");
return -1;
}
//写文件
fputc('b', pf);//fputc适用于所有输出流,这里的pf其实为文件流
fputc('i', pf);
fputc('t', pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
int main()
{
fputc('b', stdout);//stdout---标准输出流 //等价于putchar
fputc('i', stdout);
fputc('t', stdout);
return 0;
}
int main()
{
//打开文件
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
perror("fopen");
return -1;
}
//读文件
int ch = fgetc(pf);//fgetc适用于所有输出流,这里的pf其实为文件流
printf("%c", ch);
ch = fgetc(pf);
printf("%c", ch);
ch = fgetc(pf);
printf("%c", ch);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
int main()
{
int ch = fgetc(stdin);//等价于getchar scanf("%c")
printf("%c", ch);
ch = fgetc(stdin);
printf("%c", ch);
ch = fgetc(stdin);
printf("%c", ch);
return 0;
}
int main()
{
//打开文件
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
perror("fopen");
return -1;
}
//写文件
fputs("hello,world!\n", pf);//fputc适用于所有输出流,这里的pf其实为文件流
fputs("hello,bit科技!\n", pf);
fputs("hello,world!\n", pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
int main()
{
//打开文件
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
perror("fopen");
return -1;
}
//读文件
char arr[20] = { 0 };
fgets(arr,20, pf);//fgetc适用于所有输出流,这里的pf其实为文件流
printf("%s", arr);
fgets(arr ,20, pf);
printf("%s", arr);
fgets(arr, 20, pf);
printf("%s", arr);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
struct S
{
int n;
double d;
};
int main()
{
struct S s = { 100,3.14 };
//打开文件
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
perror("fopen");
return -1;
}
//写文件
fprintf(pf, "%d %lf", s.n, s.d);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
struct S
{
int n;
double d;
};
int main()
{
struct S s = { 0 };
//打开文件
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
perror("fopen");
return -1;
}
//读文件
fscanf(pf, "%d %lf", &(s.n), &(s.d));
printf("%d %lf", s.n, s.d);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
struct S
{
int n;
double d;
char name[10];
};
int main()
{
struct S s = { 100,3.14,"zhangsan"};
//打开文件
FILE* pf = fopen("data.txt", "wb");
if (pf == NULL)
{
perror("fopen");
return -1;
}
//写文件 -- 二进制方式写
fwrite(&s,sizeof(s),1,pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
struct S
{
int n;
double d;
char name[10];
};
int main()
{
struct S s = { 0 };
//打开文件
FILE* pf = fopen("data.txt", "rb");
if (pf == NULL)
{
perror("fopen");
return -1;
}
//读文件 -- 二进制方式读
fread(&s, sizeof(struct S), 1, pf);
printf("%d %lf %s\n", s.n, s.d, s.name);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
//scanf 从标准输入流(键盘)读取格式化的数据
//fscanf 从所有输入流 读取格式化的数据
//sscanf 从一个字符串 读取格式化的数据
//printf 把格式化的数据输出到标准输出流(屏幕)上
//fprintf 把格式化的数据输出到所有输出流上
//sprintf 把格式化的数据输出为字符串的形式
struct S
{
int n;
double d;
char name[10];
};
int main()
{
char arr[100] = { 0 };
struct S s = { 100,3.14,"ZHANGSAN" };
//把一个格式化的数据转换成字符串
sprintf(arr,"%d %lf %s", s.n, s.d, s.name);
printf("%s\n", arr);
struct S tmp = { 0 };
//将arr中的字符串输出为格式化的数据
sscanf(arr, "%d %lf %s", &(tmp.n), &(tmp.d), &(tmp.name));
printf("%d %lf %s\n", tmp.n,tmp.d,tmp.name);
return 0;
}
5文件的随机读写
fseek
根据文件指针的位置和偏移量来定位文件指针。
int fseek ( FILE * stream, long int offset, int origin );
ftell
返回文件指针相对于起始位置的偏移量
long int ftell ( FILE * stream );
rewind
让文件指针的位置回到文件的起始位置
void rewind ( FILE * stream );
#include<stdio.h>
int main()
{
//1. 打开文件
FILE* pf = fopen("D:\\learning\\C\\c_language\\10_8\\data.txt", "r");
if (NULL == pf)
{
perror("fopen:");
return -1;
}
//2.随机读写文件
//int ch = fgetc(pf);
//printf("%c\n", ch);
//读c
fseek(pf,2,SEEK_SET);
int ch = fgetc(pf);
printf("%c\n", ch);
//再读b
fseek(pf, -2, SEEK_CUR);
ch = fgetc(pf);
printf("%c\n", ch);
//ftell计算文件指针相对于起始位置的偏移量
int ret = ftell(pf);
printf("%d\n", ret);
//让文件指针回到起始位置
rewind(pf);
ch = fgetc(pf);
printf("%c\n", ch);
//3.关闭文件
fclose(pf);
pf = NULL;
return 0;
}
6.文本文件和二进制文件
根据数据的组织形式,数据文件被称为文本文件或者二进制文件。
数据在内存中以二进制的形式存储,如果不加转换的输出到外存,就是二进制文件。
如果要求在外存上以ASCII码的形式存储,则需要在存储前转换。以ASCII字符的形式存储的文件就是文本文件。
一个数据在内存中是怎么存储的呢?
字符一律以ASCII形式存储,数值型数据既可以用ASCII形式存储,也可以使用二进制形式存储。
如有整数10000,如果以ASCII码的形式输出到磁盘,则磁盘中占用5个字节(每个字符一个字节),而二进制形式输
出,则在磁盘上只占4个字节(VS2013测试)。
#include <stdio.h>
int main()
{
int a = 10000;
FILE* pf = fopen("test.txt", "wb");
fwrite(&a, 4, 1, pf);//二进制的形式写到文件中
fclose(pf);
pf = NULL;
return 0;
}
7.文件读取结束的判定
1.文本文件读取是否结束,判断返回值是否为EOF (fgetc),或者NULL(fgets)
例如:fgetc判断是否为EOF. fgets判断返回值是否为NULL.
2.二进制文件的读取结束判断,判断返回值是否小于实际要读的个数。
例如:fread判断返回值是否小于实际要读的个数。
int main()
{
//1. 打开文件
FILE* pf = fopen("D:\\learning\\C\\c_language\\10_8\\data.txt", "r");
if (NULL == pf)
{
perror("fopen:");
return -1;
}
//int ch = fgetc(pf);
//printf("%c\n", ch);
int ch = 0;
while (ch = fgetc(pf)!=EOF)
{
printf("%c\n", ch);
}
//3.关闭文件
fclose(pf);
pf = NULL;
return 0;
}
牢记:在文件读取过程中,不能用feof函数的返回值直接用来判断文件的是否结束。
而是应用于当文件读取结束的时候,判断是读取失败结束,还是遇到文件尾结束。
ferror 判断文件读取结束后是不是因为出现错误读取失败而结束
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int c; // 注意:int,非char,要求处理EOF
FILE* fp = fopen("test.txt", "r");
if(!fp)
{
perror("File opening failed");
return EXIT_FAILURE;
}
//fgetc 当读取失败的时候或者遇到文件结束的时候,都会返回EOF
while ((c = fgetc(fp)) != EOF) // 标准C I/O读取文件循环
{
putchar(c);
}
//判断是什么原因结束的
if (ferror(fp))
puts("I/O error when reading");
else if (feof(fp))
puts("End of file reached successfully");
fclose(fp);
fp=NULL;
}
#include <stdio.h>
#include <stdlib.h>
enum { SIZE = 5 };
int main(void)
{
double a[SIZE] = {1.0,2.0,3.0,4.0,5.0};
double b = 0.0;
size_t ret_code = 0;
FILE *fp = fopen("test.bin", "wb"); // 必须用二进制模式
fwrite(a, sizeof(*a), SIZE, fp); // 写 double 的数组
fclose(fp);
fp = fopen("test.bin","rb");
// 读 double 的数组
while((ret_code = fread(&b, sizeof(double), 1, fp))>=1)
{
printf("%lf\n",b);
}
if (feof(fp))
{
printf("Error reading test.bin: unexpected end of file\n");
}
else if (ferror(fp))
{
perror("Error reading test.bin");
}
fclose(fp);
fp = NULL; }
8.文件缓冲区
ANSIC 标准采用"缓冲文件系统"处理的数据文件的,所谓缓冲文件系统是指系统自动地在内存中为程序中每一个正在使用的文件开辟一块“文件缓冲区”。从内存向磁盘输出数据会先送到内存中的缓冲区,装满缓冲区后才一起送到磁盘上。如果从磁盘向计算机读入数据,则从磁盘文件中读取数据输入到内存缓冲区(充满缓冲区),然后再从缓冲区逐个地将数据送到程序数据区(程序变量等)。缓冲区的大小根据C编译系统决定的。