数据存储方式:
数据->变量->文件
数据
10个学生的信息:
#define N 10
struct student
{
char stu_num[15];
char stu_name[15];
float score;
}std[N]
文件的打开:
文件指针名=fopen(文件名,文件使用方式);
以字符为单位的读写操作;
字符变量=fgetc(fp);
fputc(字符变量,文件指针)
把输入的字符写到文件:
str=getchar();
while(str=' ')
{
fputc(str,fp);
str=getchar();
}
把文件读写位置指针定位到文件开头:rewind(fp)
从文件读出字符:
str=fgetc(fp);
while(str!=EOF)
{
putchar(str);
str=fgetc(fp);
}
基于字符串的读写函数:
从文件读出字符串
fgets(字符数组名,n, 文件指针);
字符串输入到文件中:
fputs(字符串,文件指针);
格式化读写函数:
fprintf(文件指针,格式控制字符串,输出地址表列);
是格式化输出到一个流/文件中;
fscanf(文件指针,格式控制字符串,输入地址表列)
数据块读写函数:
fread(buffer,size,count,fp)
存入数据到文件中
fwrite(buffer,size,count,fp)
从文件读数据;
文件的定位操作:文件的随机读写;
fseek(文件指针,位移量,起始点)
移动文件内部指针位置
n=ftell(文件指针)
获取文件指针的当前的位置。
rewind(文件指针)
将文件指针重新设置在开始处;