LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2002, 06:10 PM   #1
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Rep: Reputation: 31
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
 
Old 05-12-2002, 07:17 PM   #2
tyler_durden
Member
 
Registered: May 2001
Posts: 125

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

char buf[80];

fgets(buf, 80, stdin);
 
Old 05-12-2002, 10:52 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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;
   }
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
stdio.c cigarstub Programming 12 12-19-2014 06:39 AM
gcc and stdio in 9.1 iansworld SUSE / openSUSE 1 10-19-2004 01:35 PM
#include <stdio.h>? blackzone Programming 2 09-30-2004 06:00 AM
pthread and stdio the_dayi Programming 2 07-23-2003 01:04 AM
what package is stdio.h in? mr_vanhorn Slackware 5 07-12-2002 10:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration