Question

A tutorial is talking about cin and cout: "Syntactically these streams are not used as functions: instead, data are written to streams or read from them using the operators <<, called the insertion operator and >>, called the extraction operator."

What is a 'stream'?

Was it helpful?

Solution

Consider a "Stream" as a physical hose, or pipe. At one end, someone may pour some water in. At the other end, it will come out. This is 'reading' and 'writing' to the stream.

A stream is just a place where data goes. It can be a 'socket stream' (over the internet) or a 'file stream' (to a file), or perhaps a 'memory stream', just data written to a place in-memory (ram).

OTHER TIPS

A "stream" is an object that represents a source of data, or a place where data can be written.

Examples include file handles and pipes - things that you can read data from or write data to.

An important property of streams is that they share a common interface, so the same code can write to either a file or a pipe (for instance) without needing to be rewritten.

You should look at streams as abstractions on underlying 'sources' or 'sinks' of data. A source is something you read data from, and a sink is something you write data to.

The concept of streams allows you to perform I/O on various forms of media, network connections, pipes between applications, files, etc.

The stream abstraction is very valuable to us as developers as it allows us to simplify input and output, and it gives us the flexibility to arrange and reconnect the sources and destinations of these streams.

A good analogy is that of a hose. You can send and receive data through hoses, and you can connect these hoses to various things.

By allowing programs to talk through hoses, we allow all sorts of programs to talk to each other, and we increase interoperability and utility vastly.

This is at the heart of the UNIX philosophy, and supports some very powerful programming idioms.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top