有没有一种方法可以尽快获取文本而无需等待换行符? [英] Is there a way to get text as soon as possible without waiting for a newline?

查看:59
本文介绍了有没有一种方法可以尽快获取文本而无需等待换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C。我编写了一个非常简单的程序,该程序使用getchar()和putchar()或printf()打印回输入。有什么方法可以使用户键入一个键,程序立即注册它,而无需等待Enter键?让我显示:

I'm using C. I wrote a very simpe program which prints back the input, using getchar() and putchar() or printf(). Is there any way to make it so as soon as the user types one key, the program registers it, without waiting for an Enter? Let me show:

当前,如果用户键入 abc,然后按Enter,则程序将打印 abc和换行符,并继续等待更多输入。我想这样做,以便用户一旦输入 a,程序就会打印 a并等待更多输入。我不确定这是否必须在源代码中完成或是否必须在Windows命令行中进行某些更改。

Currently, if the user types "abc" and then presses Enter, the program prints "abc" and a newline and keeps waiting for more input. I want to make it so as soon as the user types "a", the program prints "a" and waits for more input. I'm not sure whether this has to be done inside the source code or if something has to be changed in the Windows command line.

以防万一,这是源代码代码:

Just in case, here's the source code:

#include <stdio.h>

int main()
{
    int c;

    while ((c = getchar()) != EOF) {
        putchar(c);
    }
    return 0;
}


推荐答案

(如果您使用的是Visual Studio) ,有一个名为conio( #include< conio.h> )的库,它定义了kbhit()函数和getch()。

if you are using Visual Studio, there is a library called conio (#include <conio.h>) which defines a kbhit() function and getch().

否则,在Windows上,仍然可以使用Windows SDK中的函数(ReadConsoleInput()等),但这需要更多代码(不过,一旦完成并且操作正确,就可以在需要的任何时间重复使用)

otherwise, on Windows, there is still the possibility of using functions from the Windows SDK (ReadConsoleInput() and the like), but that would require a little bit more code (although, once done and if done properly, it can be reused any time you want)

这篇关于有没有一种方法可以尽快获取文本而无需等待换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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