Portability Problem in C

I tried to create a program that writes structure elements to a binary, and then writes unique elements from the first file to another binary. I compiled it with gcc and it works very well, but with MinGW the program freezes when it tries to open and creates a second file. Do you have any ideas where the problem is?

#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",sp); 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",wp); 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; } 

EDIT: The func () function is the only problem.

EDIT: Yes, I can run it under gdb.

EDIT: sizeof (ELEM) = 84 offsetof (ELEM, p) = 80 in both cases.

+4
source share
1 answer

Wow, you won’t guess about it: aux.bin , actually all aux.* Is a reserved file name in Windows! That's why he takes forever! Take a look here so you don’t accidentally choose another reserved file name: Windows file name specification (find the page for 'aux')

+3
source

Source: https://habr.com/ru/post/1395947/


All Articles