c/c++语言开发共享使用fscanf()读取多个值(如下面的文本文件中所示)

新来的fscanf()… plz帮助

程序

#include typedef struct { int rollnum; char name[30]; int mark1; int mark2; int mark3; }data; int main(int argc,char* argv[]) { int total,c1,c2,i; char str[30]; FILE *original,*pass,*fail; data *student; original=fopen("C:\Users\user\Desktop\struct.txt","r"); pass=fopen("C:\Users\user\Desktop\pass.txt","w"); fail=fopen("C:\Users\user\Desktop\fail.txt","w"); for(i=0;irollnum, (student+i)->name, &(student+i)->mark1, &(student+i)->mark2, &(student+i)->mark3); total=student[i].mark1+student[i].mark2+student[i].mark3; if(total>50) fprintf(pass,"%d. %s %dn",c1,student[i].name,total); else fprintf(fail,"%d. %s %dn",c2,student[i].name,total); c1++,c2++; } printf("Successfuln"); fclose(original); fclose(pass); fclose(fail); return 0; } **struct.txt** 1 blesswin 20 40 50 2 sam 40 10 20 3 john 50 20 60 4 james 50 40 70 5 peter 10 40 80 

该计划是根据他们的总数将学生分组为两个文件…我似乎有一些问题,但fscanffunction…你的帮助,感谢…提前感谢

    没有任何错误,更难以确定您遇到问题的地方,但可能与您没有为您的学生分配内存这一事实有关:

     data *students; students = malloc(number_of_students * sizeof(*students)); if (students==NULL) printf("Error: failed to allocate memoryn"); 

    将数据从文件加载到分配的内存中看起来像

     for(i=0;i 

    不再忘记在不再需要后释放分配内存

     free(students); 

      以上就是c/c++开发分享使用fscanf()读取多个值(如下面的文本文件中所示)相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/519678.html

      (0)
      上一篇 2020年12月5日
      下一篇 2020年12月5日

      精彩推荐