LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   stdin buffering woes (https://www.linuxquestions.org/questions/programming-9/stdin-buffering-woes-531809/)

infinity42 02-23-2007 04:26 PM

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

tuxdev 02-23-2007 04:52 PM

Common question. You need to switch the stdin file descriptor to raw mode.

http://www.linuxquestions.org/questi...07#post2570807

infinity42 02-23-2007 05:04 PM

Thanks, apologies for asking a question that had been answered - I didn't search well enough.

Works a charm :)


All times are GMT -5. The time now is 05:43 PM.