C语言:结构体应用

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct student{
  char name[32];
  int no;
  char sex[16];
  float score;
} stu;
  
int main(int argc, char* argv[])
{
 //打开文件 
 FILE * r=fopen("A.txt","r");
 assert(r!=NULL);
 FILE * w=fopen("B.txt","w");
 assert(w!=NULL);
   
 //读写文件 
 stu a[128];
 int i=0;
 while(fscanf(r,"%s%d%s%f",a[i].name,&a[i].no,a[i].sex,&a[i].score)!=EOF)
 {
   printf("%s\t%d\t%s\t%g\n",a[i].name,a[i].no,a[i].sex,a[i].score);//输出到显示器屏幕 
   fprintf(w,"%s\t%d\t%s\t%g\n",a[i].name,a[i].no,a[i].sex,a[i].score);//输出到文件B.txt 
   i++;
 } 
   
 //关闭文件 
 fclose(r);
 fclose(w);
   
 system("pause");
 return 0;
}

 

posted @ 2021-04-11 16:51  myrj  阅读(91)  评论(0编辑  收藏  举报