LinuxQuestions.org
Visit Jeremy's Blog.
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 07-15-2006, 04:56 PM   #1
blackzone
Member
 
Registered: Jun 2004
Posts: 256

Rep: Reputation: 30
C IO question


Is there anyway to get one character at a "without" the need to press "enter".

For example, if you type "abc", the below program will display "abc\nabc".

But I want it to display "aabbcc"

Code:
#include <stdio.h>

int main()
{
  char ch;

  ch = getchar(  );
  while( 1 ) {
   putc(ch, stdout);
   ch = getchar( );
  }
}
 
Old 07-15-2006, 05:22 PM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
man setvbuf.
Did it help?
 
Old 07-15-2006, 08:48 PM   #3
blackzone
Member
 
Registered: Jun 2004
Posts: 256

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by raskin
man setvbuf.
Did it help?
ya it does. It point me to read about different buffering type in linux and lead me to find the below code.

It seemed you have to use low level IO to do it.

Code:
#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
/*------------------------------------------------*/
int getch(void) {
      int c=0;

      struct termios org_opts, new_opts;
      int res=0;
          //-----  store old settings -----------
      res=tcgetattr(STDIN_FILENO, &org_opts);
      assert(res==0);
          //---- set new terminal parms --------
      memcpy(&new_opts, &org_opts, sizeof(new_opts));
      new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
      tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
      c=getchar();
          //------  restore old settings ---------
      res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
      assert(res==0);
      return(c);
}
 
Old 07-15-2006, 10:12 PM   #4
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Your code is the correct and best way to modify the terminal behavior. Most terminals by default are in canonical mode which requres you to press enter before it processes the input, setting -ICANON allows the input to be processed almost immediately.
 
  


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
Small question about /etc/rc.local - Symbolic link question Arodef Linux - General 4 05-13-2006 02:29 AM
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 04:23 AM
Not your regular GRUB question - just a short question for a fried MBR!! ziphem Linux - General 3 01-31-2005 01:51 PM
2 part question: Speeding up MDK9.1/GNOME question wardialer Linux - Newbie 6 10-14-2004 03:16 PM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 02:02 PM

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

All times are GMT -5. The time now is 05:45 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