我正试图编写一个程序,该程序将执行线性淡入、线性淡出或线性交叉淡出(用户指定的)到“.a”文件。
我根据用户的switch输入做了一个int语句,根据输入的值,它将被定向到三种情况中的一种。
在上述每一种情况下,我都会提示用户将他/她想要用于淡出的文件的完整路径进行完整的处理,然后打开一个文件来编写文件。
然而,在文件打开后,我的程序似乎崩溃了。我很确定我正确地使用了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;
}发布于 2015-04-04 19:17:18
行FILE *f1, *f2, *fout不初始化变量。
然后,if(f2 != NULL)访问未初始化的变量。
更糟糕的是,fclose(f2)取消了一个未初始化的指针。
发布于 2015-04-04 19:10:05
如果文件未能打开,则会中断,但在切换之后,您将尝试关闭空指针。
https://stackoverflow.com/questions/29450316
复制相似问题