LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 09-01-2006, 02:41 PM   #1
astropirhana
LQ Newbie
 
Registered: Jul 2006
Distribution: Debian 3.X
Posts: 14

Rep: Reputation: 0
C getc without CR


I am trying to get input from the keyboard character by character.

I am writing my code in C.

The problem with getc and fgetc are that they force the user to enter a carriage return and then all buffered characters appear to be returned one at a time and processed. I want to process them immediatly not waiting for the carriage return key to be struck.

I found some code that mimmics the dos command kbhit().

http://www.linux-sxs.org/programming/kbhit.html

This code almost works 100%...actually 95%.

There are a few bugs. I am using it, and it is working alright. I have to work around a few shortcomings.

What is the recommended way to do this with Linux?

Astro
 
Old 09-01-2006, 03:05 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 110Reputation: 110
getc and fgetc aren't why buffering happens. Buffering is a file stream attribute.
You can switch to raw mode by doing
Code:
struct termios cur,raw;
tcgetattr(STDIN_FILENO,&cur);  // get current mode to restore later
raw=cur;
raw.c_lflag&=~(ICANON|ECHO|ISIG);
raw.c_cc[VMIN]=1;
raw.c_cc[VTIME]=0;
tcsetattr(STDIN_FILENO,TCSAFLUSH,&raw);
then return it to cooked mode by doing
Code:
tcsetattr(STDIN_FILENO,TCSAFLUSH,&cur);
You will need to #include <termios.h>
 
Old 09-01-2006, 03:12 PM   #3
astropirhana
LQ Newbie
 
Registered: Jul 2006
Distribution: Debian 3.X
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks alot

Worked great!

Astro

Last edited by astropirhana; 09-01-2006 at 03:24 PM.
 
Old 09-01-2006, 03:21 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,482

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
tuxdev is actually suggesting the same solution you already use with kbhit.

What are the 5% cases where you observe it doesn't work ?
 
Old 09-01-2006, 03:40 PM   #5
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, Arch
Posts: 572

Rep: Reputation: 64
Quote:
Originally Posted by jlliagre
tuxdev is actually suggesting the same solution you already use with kbhit.
Actually, there was one difference.
Quote:
Originally Posted by kbhit
tcsetattr(0, TCSANOW, &new_settings);
Quote:
Originally Posted by tuxdev
tcsetattr(STDIN_FILENO,TCSAFLUSH,&raw);
TCSANOW changes immediately, TCSAFLUSH discards input already received. It's not clear if this will make any difference for stdin though.
 
Old 09-01-2006, 04:03 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 110Reputation: 110
Oh yeah, didn't read that. I guess the difference might be that in this case, the simplicity of using the main library directly causes less errors than complicated mystery code in a library.

Edit: didn't refresh page to see newer comment. I'm not sure if it makes a difference, I pulled that more or less from the code example in Advanced Programming in the Unix Enviroment, originally used in a homework assignment.

Last edited by tuxdev; 09-01-2006 at 04:10 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Please teach me how to use getc(stdin) Eileen Programming 12 12-20-2004 12:34 AM


All times are GMT -5. The time now is 10:38 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration