473,698 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference between buffered and unbuffered stream?

25 New Member
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 14736
2,426 Recognized Expert Top Contributor
In what way did we fail to answer your question in your previous post?
Aug 24 '09 #2
25 New Member
ok then plz atleast tell me the difference between the functioning of cerr and cout..
Aug 24 '09 #3
2,426 Recognized Expert Top Contributor
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
25 New Member
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
2,426 Recognized Expert Top Contributor

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.


7.19.2 Files


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.


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
9,065 Recognized Expert Moderator Expert
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
25 New Member
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..
Aug 25 '09 #8
52 New Member
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

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 setbuf is not a member of
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
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 an excellent definition of what a buffered stream is right off the top of my head, but it's something like a cache that can hold data from the source, or for the destination that can't be immediately processed. Say, for example, if you are...
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++ #include <stdio.h> #include <stdlib.h> int main(void) { char buf = {0};
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 set buffer=false on a large page). Now the problem: If I turn on buffering on a large page, the page output (to the browser) is correct a few times (sometines just once, sometime 2-3 times...on the same page) then I seem to either lose data...
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 file scales 1.1M, what I did is just copy it to another output file(ten times). But I don't think I see any difference. My file system is ext3, with debian sarge(kernel-2.6.8). Here is the main part of my program:
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 ?
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 affecting the process too much. I've implemented a buffered (FileStream) copy using a queue for the bufferes. The performance is about 60% slower than the File.Copy. I've heard something about unbuffered stream, however I couldn't find anything...
by: zeeshan708 | last post by:
what is the difference between unbuffered and buffered stream??
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.