LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   input from stdio (https://www.linuxquestions.org/questions/programming-9/input-from-stdio-20797/)

raven 05-12-2002 06:10 PM

input from stdio
 
hello

i need to know how i can scan only ONE character from stdio.

i tried with getchar(), but getchar wont stop getting characters until i press enter.

but i really need only the first character of the input.

i thought of openeing a stream, and then read with read(), but i dont know how to open that stream to stdio...

ya have a good idea?

any help is appreciated.

thanks

raven

tyler_durden 05-12-2002 07:17 PM

here is an easy way to do it. set up a buffer, then use fgets. ie.

char buf[80];

fgets(buf, 80, stdin);

crabboy 05-12-2002 10:52 PM

Use fcntl to change stdin to not block. Use the O_NONBLOCK flag.
Then use the 'select' statement to wait for a single character to be read from stdin. There is an example on the select man page. What it does not cover is changing stdin not to block. The following lines will cause stdin not to block.
Code:

  int flags = fcntl( STDIN_FILENO, F_GETFL, 0 );
  if ( fcntl( STDIN_FILENO, F_SETFL, flags | O_NONBLOCK ))
  {
      perror("fcntl error");
      return -1;
  }



All times are GMT -5. The time now is 11:41 AM.