user-0 提问: 求助大佬帮忙修改代码，怎样将文件数据读入到链表，在第二次运行程序时不用手动输入直接读取显示

回复:
user: user-1
content: STUDENT *read(STUDENT *head)//读取文件函数（读文件） {     int ret;     FILE *fp;     STUDENT *p, *prev;      p = head;     if ((fp = fopen("student.txt","r"))==NULL)     {         printf("failed to open the file!!");         exit(1);     }      while (1)     {         p = (STUDENT *)malloc(sizeof(STUDENT));         if (!p)             exit(0);         ret = fscanf(fp,"%s %s %s %d %s %s %s", p->num, p->name,p->sex, &p->age,p->addr,p->major,p->xb);         if (ret < 7)             break;         if (!head) {             head = p;             prev = head;         } else {             prev->next = p;             prev = p;         }     }     free(p);     prev->next = NULL;      fclose(fp);      return head; } STUDENT *create(STUDENT *head)//创建链表，注册学生信息 {     char answer;     STUDENT *p, *pcur;     STUDENT *last = NULL;      pcur = last = head;     //system("cls");     do     {         p = (STUDENT *)malloc(sizeof(STUDENT));         if (!p)             exit(0);         printf("\n\n");         printf("----------学生信息录入---------- \n");         printf("\n");         printf("------请输入：\n");         printf("\t学号：");         scanf("%s", p->num);         printf("\t姓名：");         scanf("%s", p->name);         printf("\t性别：");         scanf("%s", p->sex);         printf("\t年龄：");         scanf("%d", &p->age);         printf("\t户籍：");         scanf("%s", p->addr);         printf("\t专业：");         scanf("%s", p->major);         printf("\t系别：");         scanf("%s", p->xb);         printf("\n");         printf("\t是否继续录入?(Y or N):   ");         getchar();   /* for '\n' */         scanf("%c", &answer);          while (pcur) {             if (pcur->num == p->num) {                 free(p);                 continue;             }             last = pcur;             pcur = pcur->next;         }           if(last) {             last->next = p;             last = last->next;         } else {             last = p;             head = last;         }     } while(answer=='Y'||answer=='y');      last->next = NULL;      save(head);    /* store infor into file */     printf("\n");     printf("\t保存成功！！！\n\n");     return head; } int main() {     STUDENT *head = NULL;     head = read(head);     //head = init();       int nam,secret;        /*登录操作使用的变量*/     printf("\n\n\n\n");     printf("\t------------------------学生学学籍管理系统------------------------\n");     printf("\n\n");     printf("\t\t\t\t账号：");     scanf("%d", &nam);     printf("\n");     printf("\t\t\t\t密码：");     scanf("%d", &secret);     //system("cls");      if(nam == 172021 && secret==172021)     {         printf("\n\n");         printf("\t\t\t\t登录成功!!!\n\n");          for(;;)         {             switch(menu_select())             {                 case 1:                     head = create(head);//注册                     break;                  case 2:                     search(head);//查询                     break;                  case 3:                     revise(head);//修改                     break;                 case 4:                     head=del(head);//删除                     break;                 case 5:                     printstu(head);//显示信息                     break;                 case 6:                     exit(0);//退出系统                     break;                 default:                     printf("输入错误");                     break;              }         }     }     else     {         printf("输入错误，请重新登录");     }    }

user: user-0
content: 谢谢你啊，我对你给的答案做了调整，主函数我并没有改动，而是在显示函数和搜索函数之前先读一遍，这样就可以不用改变原来的结构。然后退出时做一次保存，这样也可以，谢谢你给我的参考！ int main() {     STUDENT *head;     head = init();          int nam,secret;        /*登录操作使用的变量*/     printf("\n\n\n\n");     printf("\t------------------------学生学学籍管理系统------------------------\n");     printf("\n\n");     printf("\t\t\t\t账号：");     scanf("%d", &nam);     printf("\n");     printf("\t\t\t\t密码：");     scanf("%d", &secret);     //system("cls");       if(nam == 172021 && secret==172021)     {     	printf("\n\n");         printf("\t\t\t\t登录成功!!!\n\n");           for(;;)         {             switch(menu_select())             {                 case 1:                     head = create();//注册                     break;                   case 2:                 	head=read(head);                     search(head);//查询                     break;                   case 3:                     revise(head);//修改                     break;                 case 4:                     head=del(head);//删除                     break;                 case 5:                 	head=read(head);                     printstu(head);//显示信息                     break;                 case 6:                 	save(head);                     exit(0);//退出系统                     break;                 default:                        printf("输入错误");                        break;               }         }     }     else     {        printf("输入错误，请重新登录");     }

user: user-0
content: 求帮忙修改 read 函数