How to read a character from stdin without the program wait for aEnter key?

E

eap.britzkrieg

Hello,

I wanna read characters from stdin as long as I put them on it from
keyboard.

For example:

If I use

fscanf(stdin, "%c", &c);

when I put 'w', for instance, fscanf() waits until I press Enter, but
I don't wanna that this occur.

If I press 'w', I wanna that the program uses 'w' immediately.

Thanks a lot.
 
I

Ike Naar

I wanna read characters from stdin as long as I put them on it from
keyboard.

For example:

If I use

fscanf(stdin, "%c", &c);

when I put 'w', for instance, fscanf() waits until I press Enter, but
I don't wanna that this occur.

If I press 'w', I wanna that the program uses 'w' immediately.

This is a FAQ.
The answer is here: http://c-faq.com/osdep/cbreak.html
 
B

BartC

eap.britzkrieg said:
Hello,

I wanna read characters from stdin as long as I put them on it from
keyboard.

For example:

If I use

fscanf(stdin, "%c", &c);

when I put 'w', for instance, fscanf() waits until I press Enter, but
I don't wanna that this occur.

If I press 'w', I wanna that the program uses 'w' immediately.

You need a function that is specific to your OS. (Using Windows console,
it's getch(), defined in conio.h. This might save you wading through the faq
mentioned elsewhere, which seems a little dated.)
 
A

Antoninus Twink

(Using Windows console, it's getch(), defined in conio.h. This might
save you wading through the faq mentioned elsewhere, which seems a
little dated.)

A more portable solution would be ncurses, which is available for most
*nixes and also MS Windows.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top