1.打开当前目录下名叫“data.in.txt”的文件
FILE *fp=fopen("data.in.txt","r");//如果打开了的话,返回一个指针
r为只读的打开方式,不存则返回NULL
w为只写的打开方式,存则清,不存则建立
a为追加的打开方式,存则加,不存则建立
有“+”的都可读写
r+,写时:存则覆盖原有内存,不存返回NULL
w+,写时,同w
a+,写时同a
2.关闭文件
fclose(fp);
3.读取文件中所有字符
while(!feof(fp))//读取所有字符的方式,指针会自动后移
{
putchar(fgetc(fp));
}
4.往文件中写入字符串
fputs(s,fp1);
fputs("\n",fp);//写入回车
5.光标回到文件开始
rewind(fp);
6.格式化写入数据
fprintf(fp,"%d %s %c %.2lf %.2lf %.2lf %.2lf %.2lf\n",stu.number,stu.name,stu.sex,stu.score[0],stu.score[1],stu.score[2],stu.score[3],stu.score[4]);
7.以数据块的方式写入文件,文件中显示的是二进制
fwrite(&stu,sizeof(stu),1,fp);
依次为:块的地址,大小,写入 的次数,写入的文件