On Wednesday 18 February 2004 07:51, Ryan Walsh wrote:
I'm just getting back into programming, and this is my first introduction to LINUX. I was wondering if anyone had a basic C program on how to read input from the keyboard one character at a time (without having to press enter)...
This doesn't work so easily with standard C. The C library only defines getc() and fgetc() which both read a single character, but it is intentionally undefined after what user action this occurs. Since C came into existence along with Unix systems, this makes a lot of sense: Back in the old days of Unix (the early 1970s) users had terminals. They didn'd sit in front of the system console (computers where huge and noisy back then). This is why even nowadays you need to switch the user's tty (teletypes were the first terminals in the mid-60s) to a "raw" mode to prevent the user's input from being buffered until he hits the "return" key - even if that tty he is working on is really a Linux console or a xterm window. You need to fiddle with ioctl() to do that, and since there were so many different types of terminals back then a "terminal data base" (terminfo) came into existence that helps you to query a specific terminal's capabilities, to change its settings and to set the correct escape sequences to do even the most basic things (like, position the cursor). And since this is so much hassle a library was written around all that that does the dirty parts - curses, or, today's "new" and improved version, ncurses. So if you really, really still want to get single characters, read the documentation about terminal modes, terminfo, and ncurses. If it isn't really that important, you might as well accept the fact that users have to hit "return" - or simply change a message from "hit any key" to "hit return" to make your program continue. ;-) I know that C compilers on that "other" platform (whose name I will not utter here...) usually extend that plain C standard to something like cgetc() or getch() to work around that, but this is no longer standard C. Isn't it great to have that much heritage to look back upon? ;-) CU -- Stefan Hundhammer <sh@suse.de> Penguin by conviction. YaST2 Development SuSE Linux AG Nuernberg, Germany