LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-23-2007, 04:26 PM   #1
infinity42
Member
 
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Rep: Reputation: 16
stdin buffering woes


I am writing a simple IRC client, and I want to read from stdin. Currently I am selecting on file descriptor 0 thusly:
Code:
        while(1) {
                FD_SET(conn->sock, &rd);
                FD_SET(0, &rd);
                FD_SET(conn->sock, &wr);
                FD_SET(conn->sock, &ex);
                ret = select(conn->sock + 1, &rd, &wr, &ex, NULL);
                if(ret == -1)
                        return 1;
                if(FD_ISSET(conn->sock, &rd)) {
                        /* Process incoming messages */
                }
                if(FD_ISSET(0, &rd)) {
                        sz = read(0, &c, 1);
                        fprintf(stderr, "C\n");
                }
                if(FD_ISSET(conn->sock, &wr)) {
                        /* Send messages in send queue */
                }
                if(FD_ISSET(conn->sock, &ex)) {
                        fprintf(stderr, "Socket exception\n");
                        return 1;
                }
        }
I expected to see something like 'aCdCeC' if I typed ade. What actually happens is that 'ade' is echoed, then when I press enter 'CCCC' is printed (the extra C is for the line feed).

I have tried:
Code:
setvbuf(stdin, NULL, _IONBF, 0);
I was not surprised that this sis not work, as it is operating on the FILE* stream, not the file descriptor.

I want to read a character as soon as it is pressed. Any ideas?

Thanks
 
Old 02-23-2007, 04:52 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Common question. You need to switch the stdin file descriptor to raw mode.

http://www.linuxquestions.org/questi...07#post2570807
 
Old 02-23-2007, 05:04 PM   #3
infinity42
Member
 
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Original Poster
Rep: Reputation: 16
Thanks, apologies for asking a question that had been answered - I didn't search well enough.

Works a charm
 
  


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
Kaffeine always Buffering AvatarofVirgo Linux - General 1 10-13-2005 04:46 AM
pipeline buffering jk3us Linux - Software 2 12-17-2004 07:52 PM
turn off buffering suchi_s Programming 2 10-30-2004 07:14 AM
Control stdin buffering weeny Linux - General 0 10-27-2003 06:17 AM
Regarding buffering with streams karthikvina Programming 0 09-04-2003 02:48 AM

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

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