首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Eclipse CDT和getch()

Eclipse CDT和getch()
EN

Stack Overflow用户
提问于 2015-03-28 11:27:16
回答 3查看 1.5K关注 0票数 1

我试着用谷歌搜索我的问题的答案,但我似乎找不到答案。

下面是我非常简单的测试代码:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main(void) {
    char ch;

    printf("Enter character: ");
    ch = getch();
    printf("%c", ch);

    return 0;
}

当我尝试在Eclipse中运行此命令时,我甚至无法获得显示的第一个printf行,并且执行任何按键操作都不起作用。

我还尝试了fflush(stdout)和fflush(stdin),但是程序并没有按照我希望的那样运行。如果我在Visual Studio上尝试这一点,它可以完美地工作。

有人知道为什么吗?谢谢。

EN

回答 3

Stack Overflow用户

发布于 2015-03-28 12:38:43

代码语言:javascript
复制
output, for instance to the console/terminal, is buffered.   
it will not actually be output until either: 
1) a newline is output. 
2) fflush(stdout) is called.  
3) a read from stdin is performed 

using getchar() will cause the stdout output buffer
to be flushed to the console/terminal.

the final printf() is not showing for this same reason.
suggest changing the format string from "%c" to "%c\n"
票数 1
EN

Stack Overflow用户

发布于 2019-05-08 07:26:40

Eclipse控制台窗口似乎不支持键盘输入。

作为解决方法,您可以将调试会话配置为启动外部终端窗口。

在Eclipse-Oxygen上,您可以从Debug Configuration对话框中执行此操作。在[调试器]选项卡中,找到指定

对下级使用外部控制台(打开一个新的控制台窗口进行输入/输出)

在C++中使用cin时也会出现此问题。请看这个问题:c++ debug mode in eclipse causes program to not wait for cin

票数 0
EN

Stack Overflow用户

发布于 2015-03-28 12:36:25

尝试在前面添加以下行以打开控制台:

代码语言:javascript
复制
FILE * a = fopen("CON","w");
freopen("CON","w",stdout);
freopen("CON","r",stdin);
fclose(a);

祝好运!

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29313325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档