链表读取不了文件数据,求大神帮助...
写的是学生成绩管理系统,只写了一部分,但是13. 从文件读取信息并显示怎么也实现不了,求大神指点。文件里是:
4 3
201701 刘德华 60 70 80
201702 郭富城 80 90 100
201703 张学友 70 80 90
201704 黎明 75 75 75
程序代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 30 #define M 6 typedef struct student { long studentID; char studentName[10]; int score[M]; struct student *next; }STUDENT; STUDENT *head; int stunumber,subnumber; int Menu(void);//显示菜单 void Creat(STUDENT *head,int *stunumber,int *subnumber);//创建链表,输入信息 void Write(int stunumber,int subnumber);//把学生信息写入文件 void Read(int *stunumber,int *subnumber);//读取文件 int main(void) { int flag; do { flag=Menu(); switch(flag) { case 1:Creat(head,&stunumber,&subnumber);break; case 12:Write(stunumber,subnumber);break; case 13:Read(&stunumber,&subnumber);break; } }while(flag); return 0; } int Menu(void)//显示菜单 { int flag; printf("1. Input record\n"); printf("2. Caculate total and average score of every course\n"); printf("3. Caculate total and average score of every student\n"); printf("4. Sort in descending order by total score of every student\n"); printf("5. Sort in ascending order by total score of every student\n"); printf("6. Sort in ascending order by number\n"); printf("7. Sort in dictionary order by name\n"); printf("8. Search by number\n"); printf("9. Search by name\n"); printf("10. Statistic analysis for every course\n"); printf("11. List record\n"); printf("12. Write to a file\n"); printf("13. Read from a file\n"); printf("0. Exit\n"); printf("Please enter your choice:"); scanf("%d",&flag); printf("\n"); return flag; } void Creat(STUDENT *head,int *stunumber,int *subnumber)//创建链表,输入信息 { int i,j; int flag=0; STUDENT *stu,*pstu; printf("Please enter student's number and subject's number:"); do { flag=0; scanf("%d %d",stunumber,subnumber); if(*stunumber<=0||*stunumber>30||*subnumber<=0||*subnumber>6) { printf("Input error!Please input again.\n"); flag=1; } }while(flag); stu=(STUDENT *)malloc(sizeof(STUDENT)); head=stu; if(stu==NULL) { printf("Failed.\n"); exit(0); } else { printf("No.1 student,Input ID:"); scanf("%ld",&stu->studentID); printf("Input name:"); scanf("%s",stu->studentName); for(j=0;j<*subnumber;j++) { printf("Input subject %d:",j+1); scanf("%d",&stu->score[j]); } pstu=stu; } for(i=1;i<*stunumber;i++) { stu=(STUDENT *)malloc(sizeof(STUDENT)); if(stu==NULL) { printf("Failed.\n"); exit(0); } else { printf("No.%d student,Input ID:",i+1); scanf("%ld",&stu->studentID); printf("Input name:"); scanf("%s",stu->studentName); for(j=0;j<*subnumber;j++) { printf("Input subject %d:",j+1); scanf("%d",&stu->score[j]); } pstu->next=stu; pstu=stu; } } pstu->next=NULL; } void Write(int stunumber,int subnumber)//把学生信息写入文件 { int j; STUDENT *pstu=head; FILE *fp; if((fp=fopen("student.txt","w"))==NULL) { printf("Failure to open student.txt!\n"); exit(0); } fprintf(fp,"%d %d\n",stunumber,subnumber);//写入学生人数和课程数 while(pstu) { fprintf(fp,"%10ld%8s",pstu->studentID,pstu->studentName); for(j=0;j<subnumber;j++) { fprintf(fp,"%4d",pstu->score[j]); } fprintf(fp,"\n"); pstu=pstu->next; } fclose(fp); } void Read(int *stunumber,int *subnumber)//读取文件 { int j; STUDENT *pstu=head; FILE *fp; if((fp=fopen("student.txt","r"))==NULL) { printf("Failure to open student.txt!\n"); exit(0); } fscanf(fp,"%d %d",stunumber,subnumber);//读取学生人数和课程数 while(pstu) { fscanf(fp,"%10ld%8s",&pstu->studentID,pstu->studentName); printf("%10ld%8s",pstu->studentID,pstu->studentName); for(j=0;j<*subnumber;j++) { fscanf(fp,"%4d",&pstu->score[j]); printf("%4d",pstu->score[j]); } printf("\n"); pstu=pstu->next; } printf("\n"); fclose(fp); }