What is the difference between nodelay() and cbreak() in ncurses?

左心房为你撑大大i 提交于 2021-01-29 03:12:05

问题


What is the difference between nodelay() and cbreak() in ncurses ? And why use these functions because we have getch()! If i have understand getch() permits to get the key directly just after the user hit the key. So nodelay and cbreak is useless isn't it ?


回答1:


They are two completely different functions.

From the documentation:

Normally, the tty driver buffers typed characters until a newline or carriage return is typed. The cbreak routine disables line buffering and erase/kill character-processing (interrupt and flow control characters are unaffected), making characters typed by the user immediately available to the program. The nocbreak routine returns the terminal to normal (cooked) mode.

...

The nodelay option causes getch to be a non-blocking call. If no input is ready, getch returns ERR. If disabled (bf is FALSE), getch waits until a key is pressed.

So cbreak is if you want to disable line buffering, which you may want for many reasons. One is to avoid having to use fflush(stdout) after each printout. With line buffering enabled, a printout will normally not be visible on screen until a newline character is printed.

The description of nodelay is pretty self explanatory. getch will not wait, but return instantly irregardless if any key is pressed or not.



来源:https://stackoverflow.com/questions/56245062/what-is-the-difference-between-nodelay-and-cbreak-in-ncurses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!