简体   繁体   English

Dev C会编译此代码,但在文件创建过程中会显示err 3221225477

[英]Dev C compiles this code, but during file creation gives err 3221225477

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 我曾尝试使用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);
}

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! PS:如果您注意编译器警告,则很容易发现这些问题!

I think what you're trying to do is opening the file specified by your variable nomeFile and then write some strings to said file. 我认为您要尝试执行的操作是打开变量nomeFile指定的文件,然后将一些字符串写入该文件。 The call to fopen returns a file handle - you need to save it in fp ! 调用fopen返回文件句柄-您需要将其保存在fp

The code still compiles, because the statement itself is valid - it's return value is just never stored. 代码仍然可以编译,因为语句本身是有效的-它的返回值从不存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM