开发者

Read input from console in OSX

开发者 https://www.devze.com 2023-03-22 14:13 出处:网络
I\'m using scanf() to read user input on terminal in a console application. scanf waits开发者_StackOverflow中文版 until the user hits the return key to read. Is there a way to read the user input on e

I'm using scanf() to read user input on terminal in a console application. scanf waits开发者_StackOverflow中文版 until the user hits the return key to read. Is there a way to read the user input on each keystroke?


The usual way would be to use the getch function from (the Mac port of) ncurses.

Note that while getchar reads a single character, it still normally does buffered reading, so you need to press 'return'/'enter' before it'll return.


getch() returns the character stream from stdin as it is typed.


char c = getchar();

It should do the trick.

0

精彩评论

暂无评论...
验证码 换一张
取 消