1

我尝试编译这段代码:

#include <stdio.h>

void print(FILE *a)
{
int main();
int count=20;
int c;
int stop=0;
char answer;

while(!stop){
    while((c=getc(a))!=EOF){
            fprintf(stdout,"%c",c);
            if(c=='\n'){
                    count--;
                    if(!count){
                        printf("do you want continue:y=for continue/q=for quit");
                        fflush(stdin);
                            answer=getchar();
                        if(answer=='y' || answer=='Y')
                            count=20;
                        else if(answer=='Q' || answer=='q'){
                            printf("you quit this program,press any key and hit the enter to close");
                            stop=1;
                            break;
                            }
                        else{
                            printf("argument is unacceptable,rolling back action");
                            main();
                            }
                        }
                }
        }
    if(c==EOF)
        stop=1;
    }
}
void halt()/*do nothing just for halt and waiting for input*/
{
int a;

scanf("%d",&a);
}
int main()
{
FILE *in,*fopen();
char name1[25];
int a;

printf("enter the name of the file you want to show:");
scanf("%24s",name1);
in=fopen(name1,"r");
if(in==NULL){
    printf("the files doesnt exist or it is in another directory, try to enter again\n");
    main();
        }
else
    print(in);

fclose(in);
halt();

return 0;
}

真正重要的是这部分:

if(!count){
    printf("do you want continue:y=for continue/q=for quit");
    fflush(stdin);
    answer=getchar();
    if(answer=='y' || answer=='Y')
         count=20;
    else if(answer=='Q' || answer=='q'){
         printf("you quit this program,press any key and hit the enter to close");
         stop=1;
         break;
    }
    else{
         printf("argument is unacceptable,rolling back action");
         main();
    }
}

该程序预计将显示一个文件的 20 行内容,但是当我在终端中运行它时,当涉及到if-else部分时,它往往会执行 else,无论我放在answer变量中。我做错了什么

注意:我已经用不同的编译器(如 lcc-win32)尝试过这段代码,它工作正常。

4

2 回答 2

3

不确定问题出在哪里,这是基于您的代码的配对 q&d 示例,并且可以按预期工作。

#include <stdio.h>

int main(int argc, char * argv[])
{
  char  answer;

  while(1) {
    printf("\ndo you want continue:y=for continue/q=for quit: ");
    fflush(stdin);
    answer=getchar();

    if(answer=='y' || answer=='Y')
      puts("y/Y entered");
    else if(answer=='Q' || answer=='q'){
      puts("q/Q entered");
      break;
    }
    else
      puts("Something other than q/y entered.");
  }
}

不幸的是,我现在时间紧迫,所以我无法检查您的版本和我的版本之间是否有任何实质性差异.. 我不记得任何东西。

这是在 Ubuntu 下使用 gcc 4.4.3 测试的。

看看这是否适合你。

于 2012-06-28T15:57:44.360 回答
2

问题在于它fflush(stdin)没有标准化并且在不同的系统上表现不同。有关更多信息,请查看http://c-faq.com/stdio/stdinflush.htmlhttp://c-faq.com/stdio/stdinflush2.html

那么这里发生了什么:您正在使用scanf("%24s",name1);从标准输入读取数据,例如,用户输入文件名(例如test.c,按下Return.stdin现在包含test.c\n.scanf现在从标准输入读取多达 24 个字符,在我们的例子中test.c并离开\nstdin这意味着stdin包含 now \n。该fflush(stdin)调用不会从中删除\nstdin这意味着您的getchar()调用将从标准输入读取\n,它解释了所描述的行为。

标准输入内容随时间变化

  1. 之前scanftest.c\n
  2. 之后scanf\n-使用scanftest.c并将其分配给name1
  3. 之后fflush(stdin)\n
  4. 之后getchar():空getchar读并返回\n

有关此确切问题的更多信息以及更好的描述,请查看有关 fflush vs.gets 的 c-faq 条目

于 2012-06-28T17:38:04.957 回答