使用输入功能在Linux下C,没有pressing进入 [英] using input function in C on linux, without pressing enter

查看:125
本文介绍了使用输入功能在Linux下C,没有pressing进入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个code在Windows上,使用功能残培() STDIO 。事情是我必须使用不需要pressing输入输入功能。

I wrote a code on windows, using the function getch() from stdio. The thing is I have to use an input function that does not require pressing enter.

我的code编译和完美的作品上的窗口。然而,这个任务已经在Linux上运行,当我尝试这样做,它告诉我它不承认残培()(或 _getch ())。
现在的问题是,根据分配,我不能用其他的包括但 stdio.h中(以及同样为把一个标志),所以我不能使用 curses.h里来解决这个问题。

My code compiles and works perfectly on windows. However, this assignment has to run on linux, and when I try to do so, it tells me it does not recognize getch() (or _getch()). The problem is that according to the assignment, I can not use other includes but stdio.h (and same goes for adding a flag), so I can not use curses.h to solve this.

我也不能使用 termios.h 等等,我们还没有学会它。
我怎样才能解决这个问题?是否还有其他选择吗?

I also can not use termios.h and so on, we have not learned it. How can I solve this? Are there any other options?

感谢

推荐答案

在UNIX系统图书馆的做法是对我们 ncurses的的,但它是一个有点框架,并非易如反掌成立的。非库方式是把标准输入流中的非规范的使用输入模式 tcgetattr() tcsetattr ()与文件描述符 0 。键入从内存中必要的code(即我现在不能测试它,我可能忘了一些重要的东西)对应的code看起来是这样的:

The library approach on UNIXes is to us ncurses but it is a bit of a framework which isn't entirely trivial to set up. The non-library mode is to turn the standard input stream into non-canonical input mode using tcgetattr() and tcsetattr() with the file descriptor 0. Typing the necessary code from memory (i.e., I can't test it now and I probably forgot something important) the corresponding code looks something like this:

struct termios setings;
tcgetattr(0, &settings);
settings.c_lflags &= ~ICANON;
tcsetattr(0, &settings);

显然,真正的实现将验证系统调用实际上是成功的。请注意,这code 之后的std :: CIN 标准输入将立即对重点presses反应。作为一个直接后果,所有类型的搞笑的字符将被通过。例如,当Delete键使用你会看到退格字符(CTRL-H, CHAR(7))显示出来。

Clearly, a real implementation would verify that the system calls are actually successful. Note that after this code std::cin and stdin will immediately react on key presses. As a direct consequence, all kind of "funny" characters will be passed through. For example, when the delete key is used you'll see backspace characters (ctrl-H, char(7)) show up.

这篇关于使用输入功能在Linux下C,没有pressing进入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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