繁体   English   中英

APPCRASH内部开关语句-C

[英]APPCRASH Inside Switch Statement - C

我正在尝试编写一个程序,该程序将按用户指定的方式对“ .au”文件执行线性淡入,线性淡出或线性淡入淡出。

我已经根据用户的int输入做出了一个switch语句,根据输入的值,该语句将针对三种情况之一。

在每种情况下,我都会提示用户输入他/她要用于淡入淡出的文件的完整路径,然后打开一个文件将淡入写入。

但是,似乎在打开文件后,我的程序崩溃了。 我很确定自己正确使用了switch ,并且确保在最后关闭每个文件,所以我不确定为什么程序会继续崩溃。

int main()
{
    FILE *f1, *f2, *fout;
    int r1, choice;
    float dr;
    char yn = 'y', path1[100], path2[100];

    while(yn == 'y' || yn == 'Y')
    {
        printf("Fade Out(1) Fade In(2) Cross-fade(3): ");
        scanf("%i", &choice);

        printf("Specify duration of fade (in seconds): ");
        scanf("%d", &dr);
        switch(choice) 
        {
            case(1):
                printf("Please specify full path of file you want to fade out\n");
                scanf("%s", path1);
                f1 = fopen(path1, "r");
                if(f1 == NULL) 
                {
                    printf("Incorrect file path specifiation. Program terminating...\n");
                    break;
                }
                fout = fopen("out.au", "w");
                //r1 = read_header(f1, NULL, fout, choice, dr);
                printf("We've gotten this far!\n");
                break;
            case(2):
                printf("Please specify full path of file you want to fade in\n");
                scanf("%s", path1);
                f1 = fopen(path1, "r");
                if(f1 == NULL) 
                {
                    printf("Incorrect file path specifiation. Program terminating...\n");
                    break;
                }
                fout = fopen("out.au", "w");
                //r1 = read_header(f1, NULL, fout, choice, dr);
                break;
            case(3):
                printf("Specify first file used to cross-fade\n");
                scanf("%s", path1);
                printf("Specify second file used in cross-fade\n");
                scanf("%s", path2);
                f1 = fopen(path1, "r");
                f2 = fopen(path2, "r");
                if((f1 == NULL || f2 == NULL))
                {
                    if(f1 == NULL)
                        printf("Incorrect file path specification for first file. Program terminating...\n");
                    else
                        printf("Incorrect file path specification for second file. Program terminating...\n");
                    break;
                }
                fout = fopen("out.au", "w");
                //r1 = read_header(f1, f2, fout, choice, dr);
                break;
            default:
                printf("Not a valid option.\n");
                break;
        }
        fclose(f1);
        if(f2 != NULL)
            fclose(f2);
        fclose(fout);

        fflush(stdin);
        printf("Again (y/n)? ");
        scanf("%c", &yn);
    }

    return 0;
}

FILE *f1, *f2, *fout不会初始化变量。

然后, if(f2 != NULL)访问一个未初始化的变量。

更糟糕的是, fclose(f2)取消引用了未初始化的指针。

如果文件无法打开,则会中断,但是在切换之后,您尝试关闭NULL指针。

暂无
暂无

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

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