一、文件顺序读写函数及其关系示意图
文件的顺序读写函数的函数功能和适用于如下图:
内存与文件之间的函数操作以及数据的输入输出示意图如下图:
二、函数详细介绍与示范
1.fputc(字符输出函数)(写"w")
作用: 将字符character写入到文件指针stream所指的文件中去。
注意!!!返回的为int 型,返回的为字符的ASCII值
读取成功,返回字符的ASCII值,读取失败返回EOF。
示范代码:
#include<stdio.h>
int main()
{
//相对路径
//打开文件,新建test.txt文件(注意!!所创建的文件在当前工程test_10_13文件路径下)
FILE*pf = fopen("test.txt","w");
if (pf==NULL)
{
perror("fopen");
return 1;
}
//写文件
//fputc('a',pf); 将字符a写入到文件指针pf所指的文件里去。
int i = 0;
for (i = 0; i < 26;,i++)
{
fputc('a'+i,pf);
}
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
运行结果:
2.fgetc(字符输入函数)(读"r")
作用: 读取文件指针stream所指的文件中的字符。
注意!!!返回的为int 型,返回的为字符的ASCII值
读取成功,返回字符的ASCII值,读取失败返回EOF。
示范代码:
#include<stdio.h>
int main()
{
//相对路径
//打开文件,新建test.txt文件(注意!!所创建的文件在当前工程test_10_13文件路径下)
FILE*pf = fopen("test.txt","r");
if (pf==NULL)
{
perror("fopen");
return 1;
}
//读文件
int ch = fgetc(pf);
printf("%c\n",ch);
ch = fgetc(pf);
printf("%c\n", ch);
ch = fgetc(pf);
printf("%c\n", ch);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
文件内容:
打印结果:
注意!!!一个读字符后,文件中的指针自动会指向下一个字符。
优化代码:读取成功返回ASCII值,失败返回EOF。
//读文件
int ch = 0;
while ((ch=fgetc(pf))!=EOF)
{
printf("%c\n",ch);
}
3.fputs(文本行输出函数)(写"w")
作用:将字符串str写入文件中去,写入的字符串无换行(需换行可自行加\n)
示范代码;
FILE*pf = fopen("test.txt","w");
if (pf==NULL)
{
perror("fopen");
return 1;
}
//写文件,一行一行的写
fputs("hello",pf);
fputs("word", pf);
//关闭文件
fclose(pf);
pf = NULL;
运行结果:
4.fgets(文本行输入函数)(读"r")
作用:最多读取文件中的num个字符到str中去。
其中,str为读取字符所存放的位置,num为最多读取的字符数。
注意!!!真实读取的字符为num-1个!!!
代码示范:
FILE*pf = fopen("test.txt","w");
if (pf==NULL)
{
perror("fopen");
return 1;
}
//读文件,一行一行的读
char arr[] = "##########";
fgets(arr,5,pf);
//关闭文件
fclose(pf);
pf = NULL;
文件内字符:
调试结果:
代码中num =5,最大读取5个字符,实际值读取了'h','e','l''l'四个字符,所以读取的最后一个字符为‘\n’
实际读取了num-1个字符即5-1=4个字符。
所以打印的时候无需加'\n'换行,因为所读取的最后一个字符即为'\n'!!!!!
char arr[20] = "##########";
fgets(arr,20,pf);
printf("%s",arr);
fgets(arr, 20, pf);
printf("%s", arr);
//关闭文件
fclose(pf);
pf = NULL;
5.fprintf(格式化输出函数)(写"w")
作用:将数据按照相应的格式写入输出到文件中去。
fprintf对比于printf,fprintf函数多了一个参数FILE * stream。
示范代码:
#include<stdio.h>
struct s
{
char name[20];
int age;
float score;
};
int main()
{
struct s stu = { "zhangsan", 20, 90.5f }; //初始化结构体
FILE*pf = fopen("text.txt","w"); //新建文件
if (pf == NULL)
{
perror("fopen");
return 1;
}
//写文件
fprintf(pf,"%s %d %f",stu.name,stu.age,stu.score);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
文件内容:
5.fscanf(格式化输入函数)(读"r")
作用:格式化读取文件中的数据。
fscanf对比于scanf,fscanf函数多了一个参数FILE * stream。
示范代码:
#include<stdio.h>
struct s
{
char name[20];
int age;
float score;
};
int main()
{
struct s stu = { 0 };
FILE*pf = fopen("text.txt","r");
if (pf==NULL)
{
perror("fopen");
return 0;
}
//读文件
fscanf(pf,"%s %d %f",stu.name,&(stu.age),&(stu.score));
printf("%s %d %f", stu.name, stu.age, stu.score);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
运行结果:
6.fwrite(二进制输入)(写"w")
作用:从指针ptr开始所指向的数据里面,一次写上count个大小为size的数据到文件里面去。
示范代码:
#include<stdio.h>
struct s
{
char name[20];
int age;
float score;
};
int main()
{
struct s stu = {"zahngsan",20,90.5f}; //初始化结构体
FILE*pf = fopen("text.txt","wb"); //新建文件,"wb"二进制写
if (pf == NULL)
{
perror("fopen");
return 1;
}
//写文件
fwrite(&stu,sizeof(stu),1,pf);//将结构体stu写入文件中。
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
文件内容:二进制xign
7.fread(二进制输出)(读"r")
作用:从文件读取count个大小为size的数据到ptr所指向的空间里去。
示范代码:
#include<stdio.h>
struct s
{
char name[20];
int age;
float score;
};
int main()
{
struct s stu = { 0 }; //初始化结构体
FILE*pf = fopen("text.txt", "rb"); //新建文件,"rb"二进制读
if (pf == NULL)
{
perror("fopen");
return 1;
}
//写文件
fread(&stu, sizeof(stu), 1, pf);//将文件中的数据读入到stu里去。
printf("%s %d %f",stu.name,stu.age,stu.score);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
运行结果:
三、额外补充
fgetc,fputc,fgets,fputs,fscanf,fprintf适用于所有输出流/输入流,而printf,scanf只适用于标准输出流/标准输入流。
那么如何通过fgetc,fputc来实现标准标准输出流/标准输入流的输出,输入(从键盘上读,屏幕上写)呢?
对于任何一个C程序,只要运行起来,就默认打开3个流:
stdin --标准输入流--键盘
stdout--标准输出流--屏幕
stderr--标准错误流--屏幕
注意!!!!stdin,stdout,stderr都是FILE*类型。
示范代码:
#include<stdio.h>
int main()
{
int ch = fgetc(stdin); //从标准输入流输入
fputc(ch,stdout); //从标准输出流输出。
return 0;
}
运行结果: