Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a stream exactly?

When my book says : a stream is a sequence of characters read or written from a device then my book says : the istream and the ostream types represent input and output stream ( what does it mean ?) how exactly do cout and cin work?

I'm not a language native and I can't understand when my book says : the output operator writes the given value on the given ostream.

like image 505
Piero Borrelli Avatar asked Sep 03 '14 19:09

Piero Borrelli


People also ask

How do you explain a stream?

A stream is a body of water that flows on Earth's surface. The word stream is often used interchangeably with river, though rivers usually describe larger streams.

What is stream and how it works?

In simpler terms, streaming is what happens when consumers watch TV or listen to podcasts on Internet-connected devices. With streaming, the media file being played on the client device is stored remotely, and is transmitted a few seconds at a time over the Internet.

What exactly are streams in Java?

A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. The features of Java stream are – A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.

What exactly is a stream in C++?

A C++ stream is a flow of data into or out of a program, such as the data written to cout or read from cin. For this class we are currently interested in four different classes: istream is a general purpose input stream. cin is an example of an istream.


2 Answers

The fundamental idea behind the metaphor of a "stream" is that it provides or consumes data in a single-pass fashion: for example, for an input stream, data is produced precisely once. You can ask the stream for some more data, and once the stream has given you the data, it will never give you that same data again.

This is why in order to do anything meaningful with streams, you will very usually want to attach a kind of buffer (a "stream buffer", if you will) to the stream which stores some (usually small) amount of data that's been extracted from the stream in a random-access, inspectable and processable piece of memory. (There are similar, reversed ideas for output streams.)

Occasionally it makes sense to process streams without any buffering. For example, if you have an input and an output stream and you read integers from the input and write the doubled value of each integer to the output, you can do that without buffering.

So when thinking about ranges of data, streams are things that you can traverse only once and never again. If you're thinking in terms of forward progress, then streams have another property, which is that they may block: an input stream may block when it has no data, and an output stream may block when it cannot accept any more data. That way, from within the program logic, you can imagine that a input stream always contains data until its end is reached, but a program relying on that may run for an arbitrary, unbounded amount of wall-clock time.

like image 92
Kerrek SB Avatar answered Oct 11 '22 03:10

Kerrek SB


You can define it in simple words as the flow of data which can be the input flow and output flow. So you can think of it as the flow of data from a program to a file or vice versa. The below image may help you understand it better:

enter image description here

enter image description here

From MSDN

The stream is the central concept of the iostream classes. You can think of a stream object as a smart file that acts as a source and destination for bytes. A stream's characteristics are determined by its class and by customized insertion and extraction operators.

like image 38
Rahul Tripathi Avatar answered Oct 11 '22 02:10

Rahul Tripathi