CLion中的EOF错误 [英] An EOF bug in CLion

查看:112
本文介绍了CLion中的EOF错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>

/*Checking whether the value of (getchar() != EOF) is 1,
  when not reaching the EOF*/
main() {
    int c;

    printf("Please enter character:\n");
    while (c = getchar() != EOF) {
        printf("%d\t", c);
    }
    printf("%d - at EOF\n", c);
} 

我已经在CLion中运行了此代码,但是存在一个问题,直到我输入一些单词后,第一个 printf()中的内容才出现.

I have run this code in CLion, but there has been a problem that the content in the first printf() haven't appeared until I entered some words.

有一个例子.

error
^D
Please enter character:
1   1   1   1   1   1   0 - at EOF

我知道这可能是因为我在注册表中禁用了选项run.processes.with.pty,因为当该选项可用时,请输入字符:"一词在正确的位置.但是,如果我不这样做,就不能使用 Ctrl + D 发送EOF.此外,似乎只有当我在字符后的新空行中键入 Ctrl + D 时,结果才是正确的.

I know it's probably because I have disabled the option run.processes.with.pty in Registry, since the sentence 'Please enter character:' is at right place when the option is available. But if I don't do that, I can't use Ctrl+D to send an EOF. In addition, it seems the result can be correct only when I type Ctrl+D in a new empty line after characters.

平台:Windows 10,工具链:MinGW

Platform: Windows 10, Toolchain: MinGW

顺便说一句,我也尝试过Cygwin.再次出现相同的问题.有什么主意吗?

BTW, I've also tried Cygwin. The same problem occurred again. Any idea?

推荐答案

运算符优先级. @Johnny Mopp

c = getchar()!= EOF c =(getchar()!= EOF)相同,当然不是OP想要的.

c = getchar() != EOF is the same as c = (getchar() != EOF), certainly not what OP wants.

// while (c = getchar() != EOF)
while ((c = getchar()) != EOF)


但是存在一个问题,就是在我输入一些单词之前,第一个printf()的内容才出现.

but there has been a problem that the content in the first printf() haven't appeared until I entered some words.

stdout 通常被缓冲.可能是 line 缓冲或完全缓冲,或者根本没有缓冲.使用 fflush(stdout)确保输出已发布.

stdout is usually buffered. It might be line buffered or fully buffered or not at all. Use fflush(stdout) to insure output is posted.

printf("Please enter character:\n");
fflush(stdout); //add
while (c = getchar() != EOF) {
  printf("%d\t", c);
  fflush(stdout); //add
}
printf("%d - at EOF\n", c);

这篇关于CLion中的EOF错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆