1.文件
程序文件:
(1)源程序文件(后缀为.c)
(2)目标文件(windows环境后为.obj)
(3)可执行程序(windows环境后缀为.exe
数据文件:
文件的内容不一定是程序,而是程序运行时读写的数据,比如程序运行需要从中读取数据的文件,或者输出内容的文件.
2 数据文件根据数据组织形式:文本文件 二进制文件
文本文件:在外存上以ASCII码的形式存储,则在需要存储前转换,以ASCII字符的形式存储的文件。
二进制文件:数据在内存中以二进制的形式存储,如果不加转换的输出到外存的文件
3 文件在读写之前应打开文件,在使用结束之后应该关闭文件。
文件指针:FILE 是一个结构体类型,存储文件信息
//打开文件
FILE* pf = fopen(const char* filename,const char* mode);
//关闭文件
fclose(pf);
pf = NULL;//最后关闭文件,要把文件指针置为空
4 注意:若文件打开方式是只读,不存在文件则会出错;若为写,若不存在文件,则会自动创建一个新的文件
打开方式:
文件使用方式 | 含义 | 如果指定文件不存在 |
---|---|---|
“r”(只读) | 为了输入数据,打开一个已经存在的文本文件 | 出错 |
“w(只写)” | 为了输出数据,打开一个文本文件 | 建立一个新的文件 |
“a(追加)” | 向文本文件尾添加数据 | 出错 |
“rb(只读)” | 为了输入数据,打开一个二进制文件 | 出错 |
“wb(只写)” | 为了输出数据,打开一个二进制文件 | 建立一个新的文件 |
“ab(追加)” | 向一个二进制文件尾添加数据 | 出错 |
“r+(读写)” | 为了读和写,打开一个文本文件 | 出错 |
“w+(读写)” | 为了读和写,打开一个新的文件 | 建立一个新的文件 |
“a+(读写)” | 打开一个文件,在文件尾进行读写 | 建立一个新的文件 |
“rb+(读写)” | 为了读和写打开一个二进制文件 | 出错 |
“wb+(读写)” | 为了读和写,新建一个新的二进制文件 | 建立一个新的文件 |
“ab+(读写)” | 打开一个二进制文件,在文件尾进行读和写 | 建立一个新的文件 |
例如:
FILE *pf = fopen("myfile.txt","w");
5.文件的输入输出函数
功能 | 函数名 | 适用于 |
---|---|---|
字符输入函数 | fgetc | 所有输入流 |
字符输出函数 | fputc | 所有输出流 |
文本行输入函数 | fgets | 所有输入流 |
文本行输出函数 | fputs | 所有输出流 |
格式化输入函数 | fscanf | 所有输入流 |
格式化输出函数 | fprintf | 所有输出流 |
二进制输入 | fread | 文件 |
二进制输出 | fwrite | 文件 |
fputs函数
int main()
{
//fopen函数如果是以写的形式打开
//如果文件不存在,会创建这个文件
//如果文件存在,会清空文件的内容
//fopen如果是以读的形式打开,文件不存在,则打开失败!!
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;//失败返回
}
//写文件
/*fputc('a', pf);
fputc('b', pf);
fputc('c', pf);*/
fputc('a', stdout);//输出在屏幕上,和上面一样的作用
//把a-z写入文件中
int i = 0;
for (i = 'a'; i < 'z'; i++)
{
fputc(i, pf);
}
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
fgetc函数
int main()
{
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
//打开文件成功,读文件
//这是单个字符
//int ch = fgetc(pf);
//printf("%c\n", ch);//a
//ch = fgetc(pf);
//printf("%c\n", ch);//b
//ch = fgetc(pf);
//printf("%c\n", ch);//c
//利用循环输出所有字母,而不是一个一个打印
int ch = 0;
while ((ch = fgetc(pf)) != EOF)
{
printf("%c ", ch);
}
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
//fgetc也可以从键盘读入
int main()
{
int ch = fgetc(stdin);
printf("%c\n", ch);
return 0;
}
fputs函数
int main()
{
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
//写一行数据,写在文件里
//fputs("hello bit\n", pf);//可换行
//fputs("hello world", pf);
//输出在屏幕上
fputs("hello bit\n", stdout);//可换行
fputs("hello world", stdout);
fclose(pf);
pf = NULL;
return 0;
}
fgets函数
int main()
{
char arr[1024] = { 0 };
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
//读一行数据
//fgets(arr, 100, pf);//100中包含\n
//printf("%s\n", arr);
//读文件中所有数据
while (fgets(arr, 100, pf) != NULL)
{
printf("%s", arr);
}
//fgets从标准输入流中读取
fgets(arr, 100, stdin);
printf("%s\n", arr);
fclose(pf);
pf = NULL;
return 0;
}
fprintf函数
struct Stu
{
char name[20];
int age;
float score;
};
int main()
{
struct Stu s = { "zhangsan", 20, 66.5f };
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
//格式化的写入
fprintf(pf,"%s %d %f",s.name,s.age,s.score);
//fprintf(stdout,"%s %d %f",s.name,s.age,s.score);
//printf("%s %d %f", s.name, s.age, s.score);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
fscanf函数
int main()
{
struct Stu s = { "zhangsan", 20, 66.5f };
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
//格式化的读取
//fscanf(pf,"%s %d %f",s.name,&(s.age),&(s.score));//s.name本身是数组名,不用取地址
fscanf(stdin, "%s %d %f", s.name, &(s.age), &(s.score));//屏幕输出
printf("%s %d %f\n", s.name, s.age, s.score);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
sscanf函数 sprintf函数
struct Stu
{
char name[20];
int age;
float score;
};
int main()
{
struct Stu s = {"zhangsan",20,66.5f};
char buf[200] = { 0 };
//sprintf可以把结构化的数据转换为一个字符串
sprintf(buf, "%s %d %f", s.name, s.age, s.score);
printf("按照字符串的形式:%s\n", buf);
struct Stu tmp = { 0 };
//sscanf可以把一个字符串转换为一个结构化的数据
sscanf(buf,"%s %d %f",tmp.name,&(tmp.age),&(tmp.score));
printf("按照格式化的形式:%s %d %f", tmp.name, tmp.age, tmp.score);
return 0;
}
6 scanf/printf fscanf/fprintf sscanf/sprintf辨析
函数 | 作用 |
---|---|
scanf printf | 函数是应用于标准输入流和输出流的格式化输入输出语句 |
fscanf fprintf | 函数是应用于所有标准输入流和输出流格式化输入输出语句 |
sscanf sprintf | 函数是可以把结构化的数据转换为字符串,也可以从字符串中读取结构化的数据 |
😊 | 😊 |
:----: | :----: |
sprintf | 可以把结构化的数据转换为一个字符串 |
sscanf | 可以把一个字符串转换为一个结构化的数据 |
7 fwrite函数
struct Stu
{
char name[20];
int age;
float score;
};
int main()
{
struct Stu s = { "zhangsan", 20, 66.5f };
FILE* pf = fopen("data.txt", "wb");//write binary
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
//写文件,把信息写进文件
fwrite(&s, sizeof(s), 1, pf); //sizeof(s)一个元素的大小
fclose(pf);
pf = NULL;
return 0;
}
8 fread函数
struct Stu
{
char name[20];
int age;
float score;
};
int main()
{
struct Stu s = { 0};
FILE* pf = fopen("data.txt", "rb");//binary
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
//读文件-二进制
fread(&s,sizeof(struct Stu),1,pf);
printf("%s %d %f\n", s.name, s.age, s.score);
fclose(pf);
pf = NULL;
return 0;
}
9 文件读取位置
(1)定位文件指针到起始位置
int main()
{
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
return 1;
}
//读取
int ch = fgetc(pf);
printf("%c\n", ch);//a
ch = fgetc(pf);
printf("%c\n", ch);//b
//定位文件指针到起始位置
//fseek(pf, -2, SEEK_CUR);//再读取a,SEEK_CUR当前偏移地址,则为-2
fseek(pf, 0, SEEK_SET);//定位到起始位置,偏移量为0
ch = fgetc(pf);
printf("%c\n", ch);
fclose(pf);
pf = NULL;
return 0;
}
printf("%d\n", ftell(pf));//求偏移量
rewind(pf);//让文件指针回到起始位置
10 文件结束的判定
fgetc 读取的时候
EOF:(1)发生错了,结束了
(2)读取到文件末尾,结束了
fgets 读取的时候
NULL (1)发生错误
(2)读取到文件末尾,结束
fread 发现实际读取的个数小于要求读取的最大个数 - 结束了
int main()
{
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
return 1;
}
//读取
int ch = 0;
while ((ch = fgetc(pf) != EOF))
{
printf("%c ", ch);
}
//找结束的原因
if (ferror(pf))
{
printf("读取是发生错误,而失败!\n");
}
else if (feof(pf))
{
printf("遇到文件末尾,而结束的\n");
}
fclose(pf);
pf = NULL;
return 0;
}