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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top