[英]Dev C compiles this code, but during file creation gives err 3221225477
我試圖在假期做一些作業以跟上我的進度,但我陷入了這個錯誤
我曾嘗試使用GDB和Dev-C ++進行編譯,但由於不同的原因,它們都無法做到這一點
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);
}
輸出應提供一個選項,可以根據需要重復執行該循環,但是它顯示了此錯誤,並且程序迫使我退出
您打開文件,但不保存文件指針。 因此,您最終要寫入一個未初始化的文件描述符。 生成唯一崩潰的好方法! 它應該是:
fp = fopen(nomeFile,"w");
PS:如果您注意編譯器警告,則很容易發現這些問題!
我認為您要嘗試執行的操作是打開變量nomeFile
指定的文件,然后將一些字符串寫入該文件。 調用fopen
返回文件句柄-您需要將其保存在fp
!
代碼仍然可以編譯,因為語句本身是有效的-它的返回值從不存儲。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.