请大佬帮弟弟改改
请大佬帮弟弟改改,数据修改之后在score中多了一组信息“ddd 60”两名同学分别是,zhangsan,lisi 学科是aaa ,bbb,ccc,ddd,学科后面的是分数
程序代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { int i=0; char subject[50]; char a[50]; char name[50]; struct stu { char name[50]; char number[10]; char subject[50]; char score[3]; char score3[3]; }; struct stu stum; struct stu copy; FILE *fp; FILE *fp1; fp = NULL; fp1 = NULL; fp=fopen("score.txt","r+"); fp1=fopen("score1.txt","r+"); printf("请输入您所修改成绩学生的名字:"); scanf("%s",&name); printf("请输入您所修改成绩学生的科目:"); scanf("%s",&subject); while(feof(fp)==0) { fscanf(fp,"%s %s",&stum.name,&stum.number); strcpy(copy.name,stum.name); strcpy(copy.number,stum.number); fprintf(fp1,"%s %s ",copy.name,copy.number); if(strcmp(name,stum.name)==0) { //fprintf(fp1," %s %s ",stum.name,stum.number); fscanf(fp," %s %s",&stum.subject,&stum.score); strcpy(copy.subject,subject); if(strcmp(subject,stum.subject)==0) { printf("您要将该同学的%s分数改为:",copy.subject); scanf("%s",©.score); fprintf(fp1,"%s %s ",copy.subject,copy.score); } } } //fseek(fp,5,2); //rewind(fp); //rewind(fp1); fclose(fp); fclose(fp1); fp=fopen("score.txt","w"); fp1=fopen("score1.txt","r+"); while(feof(fp1)==0) { fscanf(fp1,"%s %s",&stum.name,&stum.number); strcpy(copy.name,stum.name); strcpy(copy.number,stum.number); fprintf(fp,"%s %s ",copy.name,copy.number); } fclose(fp); fclose(fp1); return 0; }
score.txt
内容:zhangsan 111 aaa 60 bbb 60
lisi 222 ccc 60 ddd 60
score.txt是空的