Buffered vs Unbuffered

This is a discussion on Buffered vs Unbuffered within the C Programming forums, part of the General Programming Boards category; Hello all ! I am reading chapter 5 of Steven's APUE. He mentions that "The standard I/O library does not ...

  1. #1
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72

    Buffered vs Unbuffered

    Hello all !

    I am reading chapter 5 of Steven's APUE. He mentions that "The standard I/O library does not buffer the characters". But he also gives the standard fputc function as an example of line buffered output.

    Isn't this contradictory? I guess I have just misunderstood the whole thing. Any explanations or references to clarifying links would be greatly appreciated.

    Thanks in advance !!

  2. #2
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    ISO/IEC 9899-1999 C99 Language Standard

    5.1.2.3 Program execution

    ...

    5) The input and output dynamics of interactive devices shall take place as specified in
    7.19.3. The intent of these requirements is that unbuffered or line-buffered output
    appear as soon as possible, to ensure that prompting messages actually appear prior to
    a program waiting for input.
    ...

    7.19.3 Files

    ...

    3) When a stream is unbuffered, characters are intended to appear from the source or at the
    destination as soon as possible. Otherwise characters may be accumulated and
    transmitted to or from the host environment as a block. When a stream is fully buffered,
    characters are intended to be transmitted to or from the host environment as a block when
    a buffer is filled. When a stream is line buffered, characters are intended to be
    transmitted to or from the host environment as a block when a new-line character is
    encountered. Furthermore, characters are intended to be transmitted as a block to the host
    environment when a buffer is filled, when input is requested on an unbuffered stream, or
    when input is requested on a line buffered stream that requires the transmission of
    characters from the host environment. Support for these characteristics is
    implementation-defined, and may be affected via the setbuf and setvbuf functions.
    ...

    7) At program startup, three text streams are predefined and need not be opened explicitly
    standard input (for reading conventional input), standard output (for writing
    conventional output), and standard error (for writing diagnostic output). As initially
    opened, the standard error stream is not fully buffered; the standard input and standard
    output streams are fully buffered if and only if the stream can be determined not to refer
    to an interactive device.

    ...
    I hope this helps.
    Maybe someone can enlighten me on "interactive devices" mentioned in the explanations above? Thanks alot!
    Last edited by Sargnagel; 10-24-2002 at 02:06 PM.

  3. #3
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Thanks alot,Sargnagel.

    I deduce from your explanation that the kind of buffering used on I/O is dependent on the stream and not on the function. So fputc() might sometimes do an unbuffered output, like when it refers to a terminal, and sometimes an unbuffered output.

    By the way, what they mean by Interactive Devices is just a terminal and any device that lets user interact with the application: keyboard, screen.
    Last edited by PutoAmo; 10-24-2002 at 02:20 PM.

  4. #4
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    I deduce from your explanation that the kind of buffering used on I/O is dependent on the stream and not on the function. So fputc() might sometimes do an unbuffered output, like when it refers to a terminal, and sometimes an unbuffered output.

    I would say so, too ... but, well, I will leave the final word on this issue to the professionals of this forum.

    By the way, what they mean by Interactive Devices is just a terminal and any device that lets user interact with the application: keyboard, screen.

    Ah! Why didn't they just write it down? Thanks alot for your help!
    Last edited by Sargnagel; 10-24-2002 at 02:32 PM.

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    I think an underflow in stdin flushes stdout.

    So if you call for input, and there are no characters waiting in the input buffer, the output one gets flushed. I think.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. buffered vs unbuffered question
    By Overworked_PhD in forum Linux Programming
    Replies: 6
    Last Post: 07-04-2008, 05:57 PM
  2. How do I input an unbuffered character via the keyboard???
    By Michael_in_Ohio in forum C Programming
    Replies: 1
    Last Post: 03-23-2008, 01:00 PM
  3. Quick stdout unbuffered question
    By Kleid-0 in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 10:31 AM
  4. exec and buffered output
    By optimus in forum C Programming
    Replies: 5
    Last Post: 04-26-2004, 04:25 AM
  5. Single unbuffered input in C++
    By ronin in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2003, 07:59 PM

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