The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Manage Ogg audio streams with OGMtools iBot UNIX and Linux RSS News 0 06-03-2008 04:40 AM
WARNING: No Memory for Streams (NSTRPAGES) Steve_93630 SCO 1 01-03-2006 12:43 PM
Help capturing and reformatting buffered and unbuffered output vikingshelmut Shell Programming and Scripting 0 09-27-2005 02:27 PM
Transparent ioctls Streams calls S.P.Prasad UNIX for Advanced & Expert Users 2 05-31-2002 12:49 AM
STREAMS alwayslearningunix UNIX for Dummies Questions & Answers 1 04-10-2001 05:11 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-27-2005
gandhevinod gandhevinod is offline
Registered User
  
 

Join Date: Jan 2005
Location: Hyderabad
Posts: 8
unbuffered streams.

#include "../ourhdr.h"

int main(void)
{
int c;
char *buf;
setvbuf(stdin,buf,_IONBF,10);
setvbuf(stdout,buf,_IONBF,10);
while((c=getc(stdin)) != EOF)
{
if(putc(c,stdout) == EOF)
err_sys("output error");
}
if (ferror(stdin))
err_sys("input error");
exit(0);
}


for the above program i expected following output.
$> ./a.out
hh
(user input is in black colour and output is indicated in blue colour)
but the output is coming after pressing return key

h<return-key>
h

see in the above code i made stdin and stdout as unbuffered streams..

so can anyone explain why the output is coming after pressing return key..
  #2 (permalink)  
Old 07-27-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,720
You have to set the tty to canonical ---

This reads one key at a time without requiring the <return>
Code:
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);
}
  #3 (permalink)  
Old 01-02-2009
JohnRRocha JohnRRocha is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 1
I too was suffering from this problem, and I hit another snag. When I tried this sample code it still wouldn't work form me, until I entered a total of 4 characters or hit return 3 times after having entered my first desired character.

After a lot of digging, I found that there is a minimum value setting in the termios structure that dictates the minimum number of characters that must be read before it passes the data to you.

The default is suppose to be 1, but in my case it was set to 4. How? I don't know, but it is.

I resolved this problem by adding the following line:
Code:
    new_opts.c_cc[VMIN]=1;
before the line:
Code:
    tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
I found this information from the termio man page, look for MIN. There are also other values such as TIME in the event that you to set a timeout -- but I leave that for additional reading.

I just wanted to point out that this was 99% perfect for me, and this 1% was all that I lacked.

This may help someone else too.

-=John
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0