I was trying to do some homework to keep up during holidays and i got stuck in this error
I've tried to compile both with GDB and Dev-C++ but neither of those could do that for different reasons
void creazioneFile(char s[12]){
FILE *fp;
char cod[10],tit[20],autore[20],editor[20],nomeFile[20];
int annoPub,risp;
sprintf(nomeFile, "%s.txt",s);
fopen(nomeFile,"w");
if(fp==NULL){
printf("Si e\' verificato un errore");
return;
}
do{
printf("\nInserire codice libro: ");
scanf("%s",cod);
printf("\nInserire titolo del libro: ");
scanf("%s",tit);
printf("\nInserire autore del libro: ");
scanf("%s",autore);
printf("\nInserire anno di pubblicazione del libro: ");
scanf("%d",&annoPub);
printf("\nInserire editore libro: ");
scanf("%s",editor);
fprintf(fp,"%s %s %s %d %s\n",cod,tit,autore,annoPub,editor);
printf("Si vuole inserire un altro record (1=si/0=no):");
scanf("%d",&risp);
}while(risp!=0);
fclose(fp);
}
The output should give an option to repeat the cycle as much as needed, but it shows this error and the program forces me to quit
You open the file but do not save the file pointer. So you end up writing into an uninitialized file descriptor. Nice way to generate unique crashes! It should be:
fp = fopen(nomeFile,"w");
PS: These sorts of issues are easily caught if you pay attention to compiler warnings!