C语言打印文件数据,用C语言输出文件内所有数据
該樓層疑似違規已被系統折疊?隱藏此樓查看此樓
#include
#include
#include "string.h" /*字符串函數*/
#include "conio.h" /*屏幕操作函數*/
#include "mem.h" /*內存操作函數*/
#include "ctype.h" /*字符操作函數*/
#include "malloc.h" /*動態地址分配函數*/
typedef struct {
char name[10];
char num [6];
float score[3];
}student ;
FILE*fp;
int i;
char tmp[10];
void write()
{ char flag;
fp=fopen("a.txt","ab+");
if(fp==NULL)
printf("文件打開失敗!");
student stu ;
for(;;)
{printf("請輸入學生的學號:");
gets(stu.num);
//getchar();
printf("請輸入學生的姓名:");
gets(stu.name);
//getchar();
printf("語文:");
scanf("%f",&stu.score[0]);getchar();
printf("英語:");
scanf("%f",&stu.score[1]);getchar();
printf("數學:");
scanf("%f",&stu.score[2]);getchar();
i=fwrite (&stu,sizeof(student),1,fp);
if(i!=1) printf("Write error!");
printf("請選擇是否繼續添加!只'y'為繼續添加,其它字母為退出添加!\n");
flag=getch();
if(flag!='y') break;
}
fclose(fp);
}
void read()
{fp=fopen("a.txt","rb+");
student stu ;
if(fp==NULL)
printf("文件打開失敗!");
else
{i=fread(&stu,sizeof(student),1,fp);
printf ("姓名 學號 語文 數學 英語\n");
{
printf("%s %s %.1f %.1f %.1f\n",stu.name,stu.num,stu.score[0],stu.score[1],stu.score[2]);
}
if(i!=1) printf("Read error!");
}
fclose(fp);
}
int main()
{
write();
read();
return 0;
}
在我先用write函數輸入兩個同學的數據后,read函數只顯示第一個同學的數據,為什么?要怎么改函數才能 輸出所有同學的數據?
總結
以上是生活随笔為你收集整理的C语言打印文件数据,用C语言输出文件内所有数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言Winmain 错误,C语言编译错
- 下一篇: android webview 设置文字