Raúl Gutiérrez Segalés wrote:
Hi, Hi
I am writing, done actually, a littl app that carries on a few things that I ask it to do. There is a point where I need my app to wait till I press any key so it can keep moving.
I tried with getchar, even doing fflush(stdin) first, but the app goes right throught it, it seems like something forces a bypass over getchar, so I decided to try read() and wait for 1 character.
fflush can only be used on writeable streams, because, actually, when
you fflush a stream, glibc calls write() on its descriptor. So,
fflush(stdin) must *not* be used (on some systems, it may even cause an
error).
What are the characters you receive after having called getchar() ??
Don't forget also that, by default, the Linux terminal is in cooked
mode. That is, you have to press <enter> before receiving any character.
For example, execute the simple program :
#include
It works great.. but is it elegant and convenient to use a system call for a usermode app ? Shouldnt I use a library wrapper?
Well, IMHO it is elegant not to mix system calls with glibc functions...
What glibc function should I use to wait for a return(enter) or any other key to be pressed?
You can simply ;) put your terminal in raw mode. man cfmakeraw will tell you what to do. You may also find some tips in the Serial Programming HOW-TO (available at http://www.linuxdocs.org/HOWTOs/Serial-Programming-HOWTO/ ) Don't forget to put your terminal back in its previous state when exiting your app. It will make the user a *lot* more happy ;)
This is C.
Thanks, Raul
I hope this was helpful, Nicolas