Thread: reading from stdin

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    The term unbuffered would mean that the file doesn't have a buffer, but rather goes and calls "read()" directly when attempting to read data, and "write()" when data is being written.

    Buffering in a FILE is useful because it reducese the amount of small read()/write() operations, but it also means that data coming in slowly would potentially have to "wait" until there is a "buffer full" to read.

    You can change the buffer behaviour by using setvbuf().

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Join Date
    Feb 2008
    hmm lot of things to learn
    ok, I'm trying something which is probably full of errors

    Infact, calling checkifdata (f) gives me a big nice segmentation fault

    edit: too ugly code has been removed
    Last edited by carlorfeo; 02-05-2008 at 05:56 PM.

  3. #18
    Join Date
    Feb 2008
    ok!!! got it finally.
    Thank you all for your attention.
    The file stream doesn't need to be buffered (or is it already? I'm still confused about that...)
    Here is a working code example for who's interested:
    (last question: would it be portable?)

    #include <iostream>
    using namespace std;
    bool checkifdata (FILE *f)
    	int fno = fileno (f);
    	fd_set fdset;
    	FD_ZERO (&fdset);		// note: fdset must be referenced here and below
    	FD_SET (fno, &fdset);
    	return select (fno + 1, &fdset, NULL, NULL, NULL);	
    int main ()
    	FILE *f = fopen ("temp.txt", "r");
    	time_t timer = time (NULL);
    	char input;
    	while (1) {
    		// this just simulates my working server
    		if ((time (NULL) - timer) >= 1) {
    			timer = time (NULL);
    			cout << ".\n";
    		if (checkifdata (f)) {
    			input = fgetc (f);			// read next char
    			if (input != -1) cout << input;		// print it if not EOF
    	fclose (f);
    	return 0;
    terminal 1:
    $ g++ -o stdin stdin.cpp
    $ ./stdin
    terminal 2:
    $ cat >> temp.txt

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >I don't know what "underflow" means exactly
    underflow is both an operation and a function. When the stream buffer is exhausted[1], meaning all characters have been read from it, underflow is called to refill the buffer for further reads.

    >And I'm just guessing that stdin would have to be unbuffered for what you want.
    That's a good guess, but it's not entirely accurate. Buffering of the input streams in C and C++ is strictly a performance thing. You can play around with the buffers, even use your own custom buffers, but that doesn't change how blocking works. Even if you set your stream to be unbuffered, reads will still block. If you don't want them to block, you have to change the canonical mode of the shell or rely on a system library rather than the standard C or C++ libraries.

    [1] Stop reading from it, you'll wear it out!
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking stdin for input
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-19-2009, 09:06 AM
  2. stdin + (ctrl+z), how i detect it?
    By Olimpo in forum C Programming
    Replies: 1
    Last Post: 09-30-2006, 05:33 AM
  3. reading from stdin
    By AngKar in forum C Programming
    Replies: 4
    Last Post: 05-03-2006, 12:14 PM
  4. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 06:41 PM
  5. Array, reading in response etc...
    By mattz in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 11:41 AM