各路大大,救救!!

大大,帮我改一改,下面的代码好不!? 拜托了!!!
(下面代码是实现对文件了的某一条记录进行修改的)
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "malloc.h"
#include "stdlib.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;
int i;
int k=0;
char filename1[15]="student.dat";
char filename2[15]="student1.dat";
p=(Pu *)malloc(sizeof(Pu));
fp=fopen("student.dat","rb");
if(fp==NULL)
{
printf("文件打开不成功");
exit(0);
}
fg=fopen("student1.dat","ab");
if(fg == NULL)
{
printf("文件打开不成功");
exit(0);
}
printf("请输入要修改信息的同学的学号\n");
scanf("%d",&i);

while(!feof(fp))
{
if(fread(p,sizeof(Pu),1,fp)!=1)
exit(0);
if (p->stu.number == i)
{
k=1;
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);
p->stu.sc.sum =p->stu.sc.c_lang+p->stu.sc.chinese+p->stu.sc.math+p->stu.sc.english; }
}
if(fwrite(p,sizeof(Pu),1,fg)!=1)
exit(0);
}
fclose(fp);
fclose(fg);


if (k==0)
printf("没有找到相关信息\n");
else
printf("修改完毕\n");

remove(filename1);
fp=fopen("student1.dat","rb");
if(fp == NULL)
{
printf("文件打开不成功");
exit(0);
}
rename(filename2,filename1);
fclose(fp);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值