主题:[讨论]如何用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");
}
/* 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");
}