C and C++ what are stream and buffer?


The terms stream and buffer are closely related in C and C++ programming.You will often come across them while discussing the topics related to input and output of data in C and C++ programming.In fact the input and output performed by C and C++ revolves mainly around stream and buffer and the functions that executes them.Here we will have detail discussion of what is stream? and what is buffer? and how are they related?.

What is stream?

From C and C++ program point of view all form of input devices such as keyboard,tapes,etc. and output devices such as computer screen,files,etc.are map using a logical data stream.For instance,the data going from your keyboard to your computer screen-when you type a program- are send through a data stream.So,to put it subtly a stream is a channel through which the data flows from your program to the environment or from the environment to your program.

Note input and output devices may change alternately depending on the condition.A file which is read from may be considered as input device but if data is written on that same file it is taken as output device.

The data in stream is handle as though they have the same uniform properties unlike the input and output devices where the data may exhibit different properties.For instance when playing a video the data taken from your video file (.mp4,.mkv,.avi,etc.) will be handled as a stream of characters and when it is render on the computer screen the data are transform into pictures and graphics and sound.But when you play an mp3 file the data is again handle as stream of characters but in the output devices all we get is a sound.

Depending on the forms of mapping a stream can support,it is classified into two types:
i)Text stream and
ii)Binary stream.

Text stream

In a text stream the data is handle as a line of characters.Every line in text stream will end with a newline character,The newline character can be a single character ‘\n’ or a combination of two characters ‘\n’ and ‘\r’ ,in some system some other character besides those two mentioned may also represent a newline character.Sometimes the line can be an empty meaning it consists of only the newline character.

To make sure that the characters read from the file is outputted correctly the data should 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.

Binary stream

In a binary stream the sequence of characters are always render exactly the same as it was read from the file.Under any implementation the data written on the file can always be read back unchanged in binary stream.However,in such stream an implementation-defined number of null characters may be appended to the end of the stream.

In Unix system there is no difference between text stream and binary stream.





what is buffer?

While working with files it is not efficient to read and write a single characters from and to input and output devices.To solve this issue a stream usually has a buffer which stores the input data before out putting the data.So by buffer we simply mean a block of memory that holds the data before rendering the data to some output devices.And note here the data stream connects the input devices to buffer and then from the buffer to the output devices.

***Note you might have come across while playing a video in Youtube,it usually occurs when your internet speed is slow,the video will stop in the midway to load.In that loading process the video data is actually stored up in the buffer area and when the buffer gets full the video resumes to play.So we call the loading process as buffering.

A stream can buffer-store data- in three ways:

i)Fully buffer:
 The characters or data in the buffer are normally transferred only when the buffer is full.

ii)Line-buffered:
 The characters in the buffer are normally transferred only when a newline character is written to the buffer, or when the buffer is full.

iii)Unbuffered:
 Characters are transferred as promptly as possible.In other words the data are read from and written to directly.


Difference between buffer and stream

The pictorial representation below shows the difference between buffer and stream.

C , C++ difference between stream and buffer

As you can clearly see that buffer storage is in the mid-way of the input and output devices and stream connects the input devices to buffer area and then the buffer area to the output devices.