scanf 不读取输入

发布于 2024-10-21 20:28:59 字数 308 浏览 1 评论 0原文

我阅读了更多有关 scanf 的线程,发现一些答案没有帮助我:

while(!comanda){
    int tmp;
    if (scanf("%d", &tmp) == 0)
        getchar();
    else{
        comanda = tmp;
        fprintf(stdout,"%d",&comanda);
        fflush(stdout);}
    }
}

问题是执行这行代码后,什么也没有发生。之后我检查了“comanda”,但它不执行。

I read more threads about scanf and I found some answers bot none helped me:

while(!comanda){
    int tmp;
    if (scanf("%d", &tmp) == 0)
        getchar();
    else{
        comanda = tmp;
        fprintf(stdout,"%d",&comanda);
        fflush(stdout);}
    }
}

The problem is that after this lines of code get executed, nothing happens. After this I have a check on "comanda" which does not execute.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微凉徒眸意 2024-10-28 20:28:59

scanf 和所有格式化输入函数的问题之一是终端往往在行模式或熟模式,API 专为原始模式设计。换句话说,scanf 实现通常在遇到换行符之前不会返回。输入被缓冲,以后对 scanf 的调用将消耗缓冲的行。考虑以下简单的程序:

#include <stdio.h>

int main() {
    int a_number;
    printf("Enter a number: ");
    fflush(stdout);
    while (scanf("%d", &a_number) != EOF) {
        printf("you entered %d\n", a_number);
        printf("Enter another number: ");
        fflush(stdout);
    }
    return 0;
}

您可以在按返回之前输入多个数字。这是运行该程序的示例。

bash$ gcc foo.c
bash$ ./a.out
Enter a number: 1 2 3 4 5 6 7 8 9 10<Return>
you entered 1
Enter another number: you entered 2
Enter another number: you entered 3
Enter another number: you entered 4
Enter another number: you entered 5
Enter another number: you entered 6
Enter another number: you entered 7
Enter another number: you entered 8
Enter another number: you entered 9
Enter another number: you entered 10
Enter another number: <Ctrl+D>bash$ 
bash$

每次调用 scanf 都会从输入流中读取一个数字,但第一次调用直到我按下 return 后才返回。其余的调用立即返回,不会阻塞更多输入,因为输入流已被缓冲,并且它可以从流中读取另一个整数。

替代方法是使用 fgets 并一次处理整行数据或使用 终端界面禁用 “规范输入处理”。大多数人使用 fgets 因为 POSIX 的终端接口部分没有在 Windows 下实现。

One of the problems with scanf and all of the formatted input functions is that terminals tend to operate in line mode or cooked mode and the API is designed for raw mode. In other words, scanf implementations generally will not return until a line feed is encountered. The input is buffered and future calls to scanf will consume the buffered line. Consider the following simple program:

#include <stdio.h>

int main() {
    int a_number;
    printf("Enter a number: ");
    fflush(stdout);
    while (scanf("%d", &a_number) != EOF) {
        printf("you entered %d\n", a_number);
        printf("Enter another number: ");
        fflush(stdout);
    }
    return 0;
}

You can enter multiple numbers before pressing return. Here is an example of running this program.

bash$ gcc foo.c
bash$ ./a.out
Enter a number: 1 2 3 4 5 6 7 8 9 10<Return>
you entered 1
Enter another number: you entered 2
Enter another number: you entered 3
Enter another number: you entered 4
Enter another number: you entered 5
Enter another number: you entered 6
Enter another number: you entered 7
Enter another number: you entered 8
Enter another number: you entered 9
Enter another number: you entered 10
Enter another number: <Ctrl+D>bash$ 
bash$

Each call to scanf read a single number from the input stream but the first call did not return until after I pressed return. The remaining calls returned immediately without blocking for more input because the input stream was buffered and it could read another integer from the stream.

The alternatives to this are to use fgets and processing entire lines of data at one time or using the terminal interface to disable "canonical input processing". Most people use fgets since the terminal interface section of POSIX is not implemented under Windows.

掩于岁月 2024-10-28 20:28:59

如果您的 scanf("%d", &tmp) 可以返回 3 个值之一

  • ,如果它返回 1 这意味着一个值被读取并放置在 tmp 中,
  • 如果它返回 < code>0 表示缓冲区中存在错误字符(您可以通过下一个 getchar() 检测并消除该字符),
  • 如果它返回 EOF表示 stdin 处于文件结束状态。无论您执行多少次 getchar() ,“文件结束”条件都不会消失,您将陷入无限循环。

还要测试 scanf 的返回值是否为 EOF。

while(!comanda){
    int tmp;
    int ret;
    ret = scanf("%d", &tmp);
    if (ret == 0)
        getchar();
    else if (ret == EOF){
        perror("stdin end-of-file");
        break; /* get out of the loop */
    }
    else {
        comanda = tmp;
        fprintf(stdout,"%d",comanda); /* & is an error */
        fflush(stdout);}
    }
}

或者,甚至更好,重做您的程序,使用 fgets() 读取整行,并使用 sscanf() 解析它。

Your scanf("%d", &tmp) can return one of 3 values

  • if it returns 1 it means a value was read and placed in tmp
  • if it returns 0 it means there was a bad character in buffer (which you detect and get rid of with the next getchar())
  • if it returns EOF it means stdin is at end-of-file condition. No matter how many getchar()s you do, the 'end-of-file' condition is not going away and you're stuck in an infinite loop.

Also test the return value from scanf for EOF.

while(!comanda){
    int tmp;
    int ret;
    ret = scanf("%d", &tmp);
    if (ret == 0)
        getchar();
    else if (ret == EOF){
        perror("stdin end-of-file");
        break; /* get out of the loop */
    }
    else {
        comanda = tmp;
        fprintf(stdout,"%d",comanda); /* & is an error */
        fflush(stdout);}
    }
}

Or, even better, redo your program to read a full line with fgets() and parse it with sscanf().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文