2012-02-11 152 views
4

我試圖編寫一個程序,將結構元素寫入二進制文件,然後將第一個文件中的唯一元素寫入另一個二進制文件。我編譯它與海灣合作委員會,它的工作非常好,但與MinGW程序凍結,當它試圖打開並創建第二個文件。你知道問題在哪裏嗎?C中的可移植性問題

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct element{ 
    char name[80]; 
    int p; 
}ELEM; 

void clear_stdin() 
{ 
    char str[255]; 
    fgets(str,255,stdin); 
} 

int create() 
{ 
    FILE *f; 
    int d=0; 
    int c; 
    int n=0; 
    ELEM s; 
    f=fopen("file.bin","wb"); 
    if(f==NULL) 
    { 
     printf("create(): Could not open file.bin for read\n"); 
     return; 
    } 
    do{ 
    printf("Add elements to file?:\n1 - yes\n2 - no\n"); 
    scanf("%d",&c); 
    if (c==1) 
    { 
     printf("Name="); 
     clear_stdin(); 
     fgets(s.name,80,stdin); 
     printf("P="); 
     scanf("%d",&s.p); 
     fwrite(&s,sizeof(ELEM),1,f); 
     n++; 
    } 
    else 
     d=1; 
    } while(d==0); 
    fclose(f); 
    return n; 
} 

void show(int n) 
{ 
    FILE *f; 
    ELEM s; 
    int i=0; 
    if(n==0) 
     return; 
    f=fopen("file.bin","rb"); 
    while(i<n) 
    { 
     fread(&s,sizeof(ELEM),1,f); 
     puts(s.name); 
     printf("\t%d\n",s.p); 
     i++; 
    } 
    fclose(f); 
} 

int add(int n) 
{ 
    FILE *f; 
    int d=0; 
    int c; 
    ELEM s; 
    f=fopen("file.bin","ab"); 
    if(f==NULL) 
    { 
     printf("add(): Could not open file.bin for append\n"); 
     return; 
    } 
    do{ 
    printf("Add elements to file?:\n1 - yes\n2 - no\n"); 
    scanf("%d",&c); 
    if (c==1) 
    { 
     printf("Name="); 
     clear_stdin(); 
     fgets(s.name,80,stdin); 
     printf("P="); 
     scanf("%d",&s.p); 
     fwrite(&s,sizeof(ELEM),1,f); 
     n++; 
    } 
    else 
     d=1; 
    } while(d==0); 
    fclose(f); 
    return n; 
} 


void func(int n) 
{ 
    FILE *f,*g; 
    ELEM v[20],w; 
    int i=0,j,k,x=0,s,gn=0,test; 
    f=fopen("file.bin","rb"); 
    g=fopen("aux.bin","wb"); 
    if((g==NULL)||(f==NULL)) 
    { 
     if(g==NULL) 
      printf("function() : Could not open aux.bin for write\n"); 
     if(f==NULL) 
      printf("function() : Could not open file.bin for read\n"); 
     return; 
    } 
    i=0; 
    while(i<n) 
    { 
     fread(&v[i],sizeof(ELEM),1,f); 

     i++; 
    } 
    for(j=0;j<n;j++) 
    { 
     for(k=j+1;k<n;k++) 
     { 
      if(v[j].p==v[k].p) 
       x=1; 

     } 
     if(x==0) 
     { 
      s=strcmp(v[j].name,v[k].name); 
      if(s!=0) 
      { 
       fwrite(&v[j],sizeof(ELEM),1,g); 
       fread(&w,sizeof(ELEM),1,g); 

       gn++; 
      } 
     } 
     x=0; 
    } 
    test=fclose(g); 
    if(test!=0) 
     printf("function() : failed to closed file g\n"); 
    test=fclose(f); 
    if(test!=0) 
     printf("function() : failed to closed file f\n"); 
    g=fopen("aux.bin","rb"); 
    if(g==NULL) 
    { 
     printf("function() : Could not open aux.bin for read\n"); 
     return; 
    } 
    if(gn==0) 
     return; 
    i=0; 
    while(i<gn) 
    { 
     fread(&w,sizeof(ELEM),1,g); 
     puts(w.name); 
     printf("\t%d\n",w.p); 
     i++; 
    } 
    fclose(g); 
} 

int main() 
{ 
    int k=0,r,n; 
    do{ 
     printf("1 - create file\n2 - add elements to file\n3 - show elements\n4 - put unique elements in another file\n5 - exit program\n"); 
     scanf("%d",&r); 
     switch(r) 
     { 
      case 1 : n=create(); break; 
      case 2 : n=add(n); break; 
      case 3 : show(n); break; 
      case 4 : func(n); break; 
      case 5 : k=1; break; 
      default : printf("Command unrecognized!\n"); 
     } 
    } while(k==0); 
    return 0; 
} 

編輯: 功能FUNC()是唯一的問題。

編輯:是的,我可以在gdb下運行它。

編輯: 在兩種情況下,sizeof(ELEM)= 84 offsetof(ELEM,p)= 80。

+3

你可以縮短代碼嗎?要求某人閱讀很多東西。 – StilesCrisis 2012-02-11 18:13:52

+1

您需要製作一個簡單的示例,最好是非交互式的,以顯示問題。很有可能,在你有一個值得在這裏發表的例子之前,你會弄清楚發生了什麼。 – 2012-02-11 18:15:58

+0

今天每半個小時,結構序列化問題如何出現? – 2012-02-11 18:21:15

回答

3

哇,你不會猜這個:aux.bin,實際上任何東西aux.*是在Windows上保留的文件名!這就是爲什麼它要永遠!看看這裏,所以你不小心選擇另一個保留的文件名: windows file name specification(搜索'aux'的頁面)

+0

謝謝!它現在有效! – user1089723 2012-02-11 19:06:24

+0

這對於CON,LPT1等也是如此。幾年前,在生產系統中發現了困難。 – Joe 2012-02-11 19:34:40