【发布时间】:2014-04-21 05:32:31
【问题描述】:
第 12 到 23 行运行。但是在添加 if 语句时实际上并没有运行。它确实编译并运行。它询问第一个 printf 语句,然后在我选择一个字符时终止。为什么会发生这种情况,我该如何解决。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[25];
FILE *fp;
printf("Enter [A] and select file or [X] to exit:"); // Prompt user to select file or exit
scanf("%c",&ch);
scanf("%c",&ch);
if (ch=='A')
{
printf("Enter the file name\n"); // if user chooses 'A' this code should run
gets(file_name);
fp = fopen(file_name,"r"); // reading file
if( fp == NULL )
{
perror("File not found.\n");
exit(EXIT_FAILURE);
}
printf("Contents of %s are:\n", file_name);
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
}
else if (ch=='X')
{
printf("Exiting program...");
exit(0);
}
}
【问题讨论】:
-
为什么一个可编译的代码应该真正起作用?非语法错误代码并不意味着它没有逻辑错误
标签: c compilation