回 帖 发 新 帖 刷新版面

主题:[讨论]如何用C语言实现链表的文件操作

如下,帮修改!!!此题让小弟头痛了几天。(此题是用链表做的)
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "malloc.h"
#include "string.h"
typedef struct score
{
    int c_lang;
    int chinese;
    int math;
    int english;
    int sum;
};
typedef struct student
{
    char name[10];
    int number;
    struct score sc;
};
typedef struct Pstu
{
    struct student stu;
    struct Pstu *next;
}Pu;
void Xiugai()/*修改 */
  {
      FILE *fp,*fg;
      Pu *p,*s;
      int i,sum;
     s=p=(Pu *)malloc(sizeof(Pu));
      fp=fopen("student.txt","rb");
       if(fp==NULL)
         {
             printf("文件打开不成功");
        exit(0);      
          }
          printf("请输入要修改信息的同学的学号\n");
              scanf("%d",&i);
          while(!feof(fp))
           {          
          if(fread(p,sizeof(Pu),1,fp)!=1)
            break;
            if(i==p->stu.number)
              {                     
                  printf("请输入学生的C语言成绩\n");
                  scanf("%d",&p->stu.sc.c_lang);            
                  printf("请输入学生的语文成绩\n");
                  scanf("%d",&p->stu.sc.chinese);            
                  printf("请输入学生的数学成绩\n");
                  scanf("%d",&p->stu.sc.math);
                  printf("请输入学生的英语成绩\n");
                  scanf("%d",&p->stu.sc.english);
                  sum=p->stu.sc.c_lang+p->stu.sc.chinese+p->stu.sc.math+p->stu.sc.english;
                  p->stu.sc.sum=sum; 
                  printf("修改完毕\n");              
                 }
               
                fg=fopen("student.txt","ab");
                  if(fg==NULL)
                    {
                           printf("文件打开不成功\n");
                      exit(0);      
                       }                                   
            if(fwrite(p,sizeof(Pu),1,fg)!=1)
               break;                  
               fclose(fg);
               exit(0);
        }
            fclose(fp);                                               
           printf("没有找到相关信息\n");               
  }

回复列表 (共1个回复)

沙发

存取时,使用struct student,不要用pu,否则连指针也存进去就乱套了。

我来回复

您尚未登录,请登录后再回复。点此登录或注册