gpt4 book ai didi

c - APPCRASH 内部开关语句 - C

转载 作者:太空宇宙 更新时间:2023-11-04 07:09:38 24 4
gpt4 key购买 nike

我正在尝试编写一个程序,该程序将根据用户的指定对“.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) 解引用了一个未初始化的指针。

关于c - APPCRASH 内部开关语句 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29450316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com