472,119 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

difference between buffered and unbuffered stream?

what is the difference between the buffered and unbuffered stream ??(e.g we say that cout is buffered and cerr is unbuffered stream) does that mean that the output sent to buffered stream have to go through a buffer (i.e a temporary storage registers) ,,
if so then what is the big deal in unbuffered stream??
Aug 24 '09 #1
8 14475
donbock
2,425 Expert 2GB
In what way did we fail to answer your question in your previous post?
Aug 24 '09 #2
ok then plz atleast tell me the difference between the functioning of cerr and cout..
Aug 24 '09 #3
donbock
2,425 Expert 2GB
@zeeshan708
You're asking a fairly broad question and it would take awhile to give you a complete answer. However, I suspect that you have a more specific and precise question in mind that can be answered succinctly.

Please tell me what you understand about the difference between buffered and unbuffered I/O and highlight where you have questions. Then I can give you a more focused answer.
Aug 24 '09 #4
ok,,,i shall try to be really specific

i read in deitel's book that cerr is a unbuffered stream and cout is buffered, i thought both display output message on the monitor then why is cerr not buffered???
Aug 24 '09 #5
donbock
2,425 Expert 2GB

7.19.2 Streams

Input and output, whether to or from physical devices such as terminals and tape drives, or whether to or from files supported on structured storage devices, are mapped into logical data streams, whose properties are more uniform than their various inputs and outputs. Two forms of mapping are supported, for text streams and for binary streams.

A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character. Whether the last line requires a terminating new-line character is implementation-defined. Characters may have to be added, altered, or deleted on input and output to conform to differing conventions for representing text in the host environment. Thus, there need not be a one-to-one correspondence between the characters in a stream and those in the external representation. Data read in from a text stream will necessarily compare equal to the data that were earlier written out to that stream only if: the data consist only of printing
characters and the control characters horizontal tab and new-line; no new-line character is immediately preceded by space characters; and the last character is a new-line character. Whether space characters that are written out immediately before a new-line character appear when read in is implementation-defined.

<snip>

7.19.2 Files

<snip>

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.

<snip>

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.
This is what the C99 Specification says about buffered versus unbuffered streams and the three standard streams. You are concerned with C++, but I expect there to be little difference.

Does this answer any of your questions?
Aug 24 '09 #6
Banfa
9,065 Expert Mod 8TB
As to the question of why standard error is not buffered and standard out is just think about it. Buffered output tends to be faster and more efficient so you might expect generally all I/O to be buffered.

In fact I can remember when I first started programming the company I worked with did not use the standard library functions for reading files (do not ask me why) but instead used low level functions provided by DOS. These functions where unbuffered. There has a noticeable difference in reading a file 1 byte at a time and reading a file 512 bytes (a disk sector) at a time because the disk had to read the 512 bytes anyway and extract the required byte so the 1 byte at a time method read 512 times as much data.

So the question becomes if buffer I/O is faster and more efficient why wouldn't you use it and the answer is simple. If your I/O is buffered then you have the possibility in the situation of the program crashing that you send something for transmission and it only gets as far as the buffer before the program crashes, that is it is never actually output. If the thing being sent is critical debug data that will allow you to identify the cause of the crash then that would be quite irritating. So it is often the case that you don't want diagnostic information buffered you want it output immediately (or at least as soon as possible).

So it becomes clear, standard in and standard out are designed for normal I/O and so use buffered operation (if possible) to gain the efficiencies of using a buffer. Standard Error, however, is designed for diagnostic information and so it uses unbuffered (or not fully buffered) operation to attempt to ensure that diagnostic messages are always displayed.
Aug 24 '09 #7
very well sir.
now i got idea that for the better performance we use buffer.
more over in case of standard stream cout is buffered bcoz normally we want the best performance and in case of cerr we want no intervention so data is directly sent or received.
plz reply if u think that i have got the point..
regards.
Aug 25 '09 #8
Even I got the concept of buffer and unbuffer methodology Thanks donbock...
Aug 25 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Rich | last post: by
7 posts views Thread by Steven T. Hatton | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.