首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >APPCRASH内开关语句C

APPCRASH内开关语句C
EN

Stack Overflow用户
提问于 2015-04-04 19:03:58
回答 2查看 36关注 0票数 0

我正试图编写一个程序,该程序将执行线性淡入、线性淡出或线性交叉淡出(用户指定的)到“.a”文件。

我根据用户的switch输入做了一个int语句,根据输入的值,它将被定向到三种情况中的一种。

在上述每一种情况下,我都会提示用户将他/她想要用于淡出的文件的完整路径进行完整的处理,然后打开一个文件来编写文件。

然而,在文件打开后,我的程序似乎崩溃了。我很确定我正确地使用了switch,并且确保在结束时关闭每个文件,所以我不知道为什么程序总是崩溃。

代码语言:javascript
复制
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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-04 19:17:18

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

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

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

票数 2
EN

Stack Overflow用户

发布于 2015-04-04 19:10:05

如果文件未能打开,则会中断,但在切换之后,您将尝试关闭空指针。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29450316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档