getting a char in c without pressing “enter”

2019-07-20 21:49发布

Is it possible? Using regular scanf or getchar won't work because I have to press "enter" after entering the char.

标签: c scanf
5条回答
我命由我不由天
2楼-- · 2019-07-20 22:23

You're approaching the domain of ncurses.

查看更多
劫难
3楼-- · 2019-07-20 22:24

The input stream in C is buffered.

A buffer is a place where data is temporarily held until it is ready to be processed.

The buffer is flushed (emptied) when one of three things happens:

  • The buffer is full.
  • Data is to be printed to the standard output.
  • The buffer is flushed explicitly. (Which is what pressing the enter key does).

You're going to have to use facilities not included in the standard library in order to have that functionality.

查看更多
Luminary・发光体
4楼-- · 2019-07-20 22:27

Maybe getch() in conio.h is what you need, but note that it's non-standard.

查看更多
别忘想泡老子
5楼-- · 2019-07-20 22:37

It is not possible by using pure standard C. You will have to use a system-specific library like conio.h or the Windows API etc.

查看更多
地球回转人心会变
6楼-- · 2019-07-20 22:39

The streaming functionality is getting in the way. There may be an fcntl option to turn off the buffering. Or you may have better luck using the lower level read call.

查看更多
登录 后发表回答