Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++-What is the need of both buffer and stream?

Tags:

c++

stream

Although i have read about buffer and stream and it's working with files in c++ but i don't know what is the need of a buffer if a stream is there, stream is always there to transfer the data of one file to the program. So why do we use buffers to store data(performing same task that stream does) and what are buffered and unbuffered stream.

like image 865
Sharma Avatar asked Dec 18 '22 22:12

Sharma


1 Answers

Consider a stream that writes to a file. If there were no buffer, if your program wrote a single byte to the stream, you'd have to write a single byte to the file. That's very inefficient. So streams have buffers to decouple operations one on side of the stream from operations on the other side of the stream.

like image 89
David Schwartz Avatar answered Dec 24 '22 02:12

David Schwartz