使用结构的fscanf和fprintf的问题

时间:2015-04-06 18:19:17

标签: c struct printf scanf

int main()
{
   char heading1[30], heading2[30], heading3[30], heading4[30], heading5  [30], heading6[30], heading7[30], heading8[30], heading10[30], heading11[30], heading12[30], heading13[30], heading14[30], heading15[30];
   typedef struct student
   {
      char first[30], last[30];
      float stdnum, asg1, asg2, asg3, asg4, asg5, lab1, lab2, lab3, lab4, lab5, mid, fin;
   }student;

   student std1;
   int x;
   stdinfo=fopen("coursemarks.txt", "r");
   temp=fopen("coursemarks2.txt", "w");
   fscanf(stdinfo, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", heading1, heading2, heading3, heading4, heading5, heading6, heading7, heading8, heading10, heading11, heading12, heading13, heading14, heading15);
   fprintf(temp, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", heading1, heading2, heading3, heading4, heading5, heading6, heading7, heading8, heading10, heading11, heading12, heading13, heading14, heading15);//prints the marks and student number in the new file
   while (fscanf(stdinfo, "%d%s%s%d%d%d%d%d%d%d%d%d%d%d%d", & std1.stdnum, std1.last,  std1.first, & std1.asg1, & std1.asg2, & std1.asg3, &std1.asg4, &std1.asg5, &std1.lab1, & std1.lab2, &std1.lab3, &std1.lab4, &std1.lab5 , &std1.mid, & std1.fin)!=EOF)//creates a while statement which keeps scaning aand printing information until the scaned file comes to an end
   {
      printf(temp, "%d\t\t\t%s\t\t\t%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", std1.stdnum, std1.last, std1.first, std1.asg1, std1.asg2, std1.asg3, std1.asg4, std1.asg5, std1.lab1,  std1.lab2, std1.lab3, std1.lab4, std1.lab5 , std1.mid,  std1.fin);//prints the marks and student number in the new file
   }
   printf("What is the student number:\t");
   scanf("%d", & x);
   std1.stdnum=x;
   printf("What is the student's last name:\t");
   scanf("%s", & std1.last);
   printf("what is the student's first name?\t");
   scanf("%s", & std1.first);
   printf("What is the first assignment mark:\t");
   scanf("%d", & x);
   std1.asg1=x;
   printf("What is the second assignment mark:\t");
   scanf("%d", & x);
   std1.asg2=x;
   printf("What is the third assignment mark:\t");
   scanf("%d", & x);
   std1.asg3=x;
   printf("What is the fourth assignment mark:\t");
   scanf("%d", & x);
   std1.asg4=x;
   printf("What is the fifth assignment mark:\t");
   scanf("%d", & x);
   std1.asg5=x;
   printf("What is the first lab mark:\t");
   scanf("%d", & x);
   std1.lab1=x;
   printf("What is the second lab mark:\t");
   scanf("%d", & x);
   std1.lab2=x;
   printf("What is the third lab mark:\t");
   scanf("%d", & x);
   std1.lab3=x;
   printf("What is the fourth lab mark:\t");
   scanf("%d", & x);
   std1.lab4=x;
   printf("What is the fifth lab mark:\t");
   scanf("%d", & x);
   std1.lab5=x;
   printf("What is the midterm mark:\t");
   scanf("%d", & x);
   std1.mid=x;
   printf("What is the final exam mark:\t");
   scanf("%d", & x);
   std1.fin=x;
   fprintf(temp, "%d\t\t\t%s\t\t\t%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", std1.stdnum, std1.first, std1.last, std1.asg1, std1.asg2, std1.asg3, std1.asg4, std1.asg5, std1.lab1,  std1.lab2, std1.lab3, std1.lab4, std1.lab5 , std1.mid,  std1.fin);//prints the marks and student number in the new file
   stdinfo=fopen("coursemarks.txt", "w");
   fclose(stdinfo);
   fclose(temp);
   temp=fopen("coursemarks2", "r");
   fscanf(temp, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", heading1, heading2, heading3, heading4, heading5, heading6, heading7, heading8, heading10, heading11, heading12, heading13, heading14, heading15);
   fprintf(stdinfo, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", heading1, heading2, heading3, heading4, heading5, heading6, heading7, heading8, heading10, heading11, heading12, heading13, heading14, heading15);//prints the marks and student number in the new file

   while (fscanf(temp, "%d%s%s%d%d%d%d%d%d%d%d%d%d%d%d\n", &std1.stdnum, std1.first, std1.last, & std1.asg1, & std1.asg2, & std1.asg3, & std1.asg4, & std1.asg5, & std1.lab1, & std1.lab2, & std1.lab3, & std1.lab4, & std1.lab5 , & std1.mid, & std1.fin)!=EOF)//prints the marks and student number in the new file
   {
      fprintf(stdinfo, "%d\t\t\t%s\t\t\t%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", std1.stdnum, std1.first, std1.last, std1.asg1, std1.asg2, std1.asg3, std1.asg4, std1.asg5, std1.lab1,  std1.lab2, std1.lab3, std1.lab4, std1.lab5 , std1.mid,  std1.fin);//prints the marks and student number in the new file
   }
}

我知道问题是基于扫描文件并打印到文件,但我不知道为什么。我知道如何使用结构和扫描打印,我之前也使用过文件。我对编程很新。我知道我可以通过使用结构并使其工作正常来做到这一点,但是因为我坚持它我真的想找出使用​​结构类型的问题。起始文件有一行标题,我知道这些标题是正确扫描的,但不相信它们也是打印的。所有其他行都有学生编号,姓氏和名字,以及12个等级。

0 个答案:

没有答案