stdin buffer size

This is a discussion on stdin buffer size within the C Programming forums, part of the General Programming Boards category; I have a programm similar to this Code: char line[1024]; while (1) { if (!fgets(line,1024,stdin)) continue; func(line) } Currently 'func' ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    145

    stdin buffer size

    I have a programm similar to this


    Code:
    char line[1024];
    
    while (1) {
          if (!fgets(line,1024,stdin)) continue;
          func(line)
    }

    Currently 'func' is quit slow. My program does not work propertly when It receives data from stdin in a fast way. It looks like internal stdin buffer fill and I miss data.
    However, if the data is receive in a slow manner, func has time to process all data.

    Is there any way I can increase the stdin buffer length?
    Or is there any way I can solve this problem?

    Thank you very much.
    Regards

    FS

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I doubt that you have a situation where input is going missing. It is perfectly valid to read from the input, wait 6 minutes (or 2 days, or 9 months while a baby is formed until it's born, if you like).

    So unless you are using some OS that is really obscure (small time RTOS or some such).

    --
    Mats
    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.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,273
    The stdio buffer size definitely has nothing to do with this problem. The buffer size impacts performance, not behavior.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not sure, but:
    Code:
    while (1) {
          if (!fgets(line,1024,stdin)) continue;
          func(line)
    }
    will create an infinite loop that consumes all available CPU power (of one CPU(-core)) if you ever hit an EOF. Are you sure the "continue" shouldn't be a break?

    --
    Mats
    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.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    matsp you beat me. I was thinking if continue was best used here? Also what is meant by a "fast way"...how are you testing this?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Just a guess: the OP intends to try again if fgets() doesn't get anything -- which is conceptually wrong, since there would be a real reason for the NULL return, and fgets will not return NULL because it is "waiting" for input (it will wait). I don't use fgets, but I can't see how it would ever return NULL from stdin anyway, presuming stdin is the keyboard and not from redirection.

    The real problem must lie in func().

    If by fast vs. slow input you mean something to do with the keyboard, you are way off base -- it is not possible to type fast enough to be anything but "incredibly slow" to the computer, so that cannot be a factor.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    fgets returns NULL from stdin if:
    1. stdin is redirected to a file or device and the file/device has been read to the end.
    2. if stdin is not redirected, when the user presses the CTRL-Z (Windows, DOS, CP/M and a few others) or CTRL-D (Unix and Linux and such) to indicate "I'm done entering stuff here".
    3. If something ELSE goes wrong (I'm not sure exactly what would cause this, but at least in theory things CAN go wrong inside the stdio functions!).

    --
    Mats
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. Getting the size of the client recv buffer
    By Niara in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-27-2006, 12:46 PM
  3. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 08:45 PM
  4. Largest screen buffer size?
    By Ash1981 in forum C Programming
    Replies: 2
    Last Post: 01-30-2006, 04:31 AM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21