Welcome, Guest. Please login or register.

Author Topic: Reading one char from standard Input without waiting for CR  (Read 1676 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Stedy

  • Sr. Member
  • ****
  • Join Date: Jul 2002
  • Posts: 259
    • Show all replies
    • http://www.ianstedman.co.uk
Re: Reading one char from standard Input without waiting for CR
« on: September 14, 2007, 10:14:38 PM »
Hi,

If you are using C, the setbuf() or better, the setvbuf() function will do what you want.

the command
setvbuf(fp,NULL,_IONBF,0);

Will turn off buffering in a console window, you can then use getchar() or similar to read the character thus:

Example program (tested with Dice C, copied from the examples)

#include
#define BUFFER_SIZE 128
main()
{
   int c;
   char buf[256];
 
   setvbuf(stdin,NULL, _IONBUF,0);
   printf("Type a character: ");
   fflush(stdout);
   c=getchar();
   printf("c=%d\n");

return(0);
}

Ian