首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

各路大大,救救!

2012-08-27 
各路大大,救救!!大大,帮我改一改,下面的代码好不!? 拜托了!!!(下面代码是实现对文件了的某一条记录进行修

各路大大,救救!!
大大,帮我改一改,下面的代码好不!? 拜托了!!!
(下面代码是实现对文件了的某一条记录进行修改的)
/* 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);
  
}

热点排行