0

我已经用数据填充了一个结构并将其写入二进制文件。问题是,当我尝试读回它时,它没有正确显示结构!这是代码...

#define MAXS 30
 typedef struct {
    char  id[50];
    int pct1, pct2;
    } TStudd;

int ReadStruct(FILE* f, TStudd as ){
 int i=0;
 while (i < MAXS && fscanf(f, "%s" , as.id) == 1) 
 {
     fscanf (f, "%i %i", &as.pct1, &as.pct2);
     i++;
     } 
return i;//total number of read people
}

int WriteBinary(FILE* f, TStudd as){
 int pct1, pct2, i;
 pct1 = as.pct1;
 pct2 = as.pct2;
 i = fwrite(as.id, sizeof(as.id), 1,f);
 if( i != 1) return -1;
 i = fwrite(&pct1, sizeof(pct1), 1,f);
 if( i != 1) return -2;
 i = fwrite(&pct2, sizeof(pct2), 1,f);
 if( i != 1) return -3;
 return 0;
 }

int ReadBinary(FILE* f){
 char  id [50];
 int pct1, pct2, i;
 i = fread(id, sizeof(id), 1,f);
 if( i != 1) return -1;
 printf("%s", id);
 i = fread(&pct1, sizeof(pct1), 1,f);
 if( i != 1) return -2;
 printf("%i", pct1);
 i = fread(&pct2, sizeof(pct2), 1,f);
 if( i != 1) return -3;
 printf("%i", pct2);
 return 0;
 }

 g = fopen(sursa, "rt");

   v = ReadStruct(g, ps);
 printf("%i\n", v);
 f = fopen(dest, "w+b");

}
if ( ReadBinary(f, ps) < 0){
   printf("Eroare scriere\n");
   return 1;
   }
if ( WriteBinary(f) < 0 ){
   printf("Eroare citire\n");
   return 1;
   };
  fclose(g);
 fclose(f);

我应该改变什么?谢谢!
平台是 Windows 7 64 位,文件不受任何转换(只是从中写入和读取内容)

4

0 回答 0