将一些格式化的数据写入文本文件,…

#include<stdio.h>
main()
{
 FILE *fp;
 int i;
 struct stu       //定义结构体类型
 {
  char name[15];
  char num[6];
  float score[2];
 }student;                               //说明结构体变量
 if((fp=fopen("test.txt","w"))==NULL)    //以文本只写方式打开文件
 {
 printf("can't open file,strike any key exit!");
 exit(0);
 }
 printf("input data:\n");
 for(i=0;i<2;i++)
 {
  scanf("%s%s%f%f",student.name,student.num,&student.score[0],&student.score[1]);    //从键盘上输入
  fprintf(fp,"%s%s%7.2f%7.2f",student.name,student.num,student.score[0],student.score[1]);
                                                                    //写入文件
 fclose(fp);                                                          //关闭文件
 if((fp=fopen("test.txt","r"))==NULL)                                 //以文本只读方式重新打开文件
 {
  printf("cannot open file");
  exit(0);
 }
 printf("output from file:\n");
 while(fscanf(fp,"%s%s%f%f\n",student.name,student.num,&student.score[0],&student.score[1])!=EOF);//从文件读入
 printf("%s %s %7.2f %7.2f\n",student.name,student.num,student.score[0],student.score[1]); //显示到屏幕
 fclose(fp);                           //关闭文件
}

}将一些格式化的数据写入文本文件,再从该文件中以格式化方法读出显示到屏幕上,其格式化数据是两个学生

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值