473,418 Members | 2,075 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 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 14692
donbock
2,426 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,426 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,426 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

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

Similar topics

5
by: Rich | last post by:
Does anyone know the correct way of opening an unbuffered output file using STL? I attempted: ofstream myfile("fname.txt", ios::binary); myfile.setbuf(NULL, 0); and I was informed that...
1
by: | last post by:
Is this even possible? I've found some references to specific "unbuffered" type methods that exist in older incarnations of basic_streambuf but not in newer ones. Info please. :P
7
by: Steven T. Hatton | last post by:
I haven't looked very closely, but from what I'm seeing, it looks like there are no buffered I/O streams in the Standard Library. There are stream buffers, but not buffered streams. I don't have...
9
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int...
3
by: Brad | last post by:
I have a response filter which injects "standard" html into my pages. The filter works fine when the initial stream is small enough not to buffer...or....if I have a large unbuffered stream (i.e. I...
4
by: pank7 | last post by:
hi everyone, I have a program here to test the file IO(actually output) with buffer turned on and off. What I want to see is that there will be obvious differece in time. Here I have an input...
7
by: Cell | last post by:
when both are connected to screen and the anything written to these two constant file pointers will go onto the screen ?
1
by: LiorO | last post by:
Hi, I need to implement a high performance file copying. The File.Copy method in .NET is fast enough however I need to control the speed since it should be performed in the background without...
6
by: zeeshan708 | last post by:
what is the difference between unbuffered and buffered stream??
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.