What is a stream in C++?

C++StreamFilestreamFstream

C++ Problem Overview


I have been hearing about streams, more specifically file streams.

So what are they?

Is it something that has a location in the memory?

Is it something that contains data?

Is it just a connection between a file and an object?

C++ Solutions


Solution 1 - C++

The term stream is an abstraction of a construct that allows you to send or receive an unknown number of bytes. The metaphor is a stream of water. You take the data as it comes, or send it as needed. Contrast this to an array, for example, which has a fixed, known length.

Examples where streams are used include reading and writing to files, receiving or sending data across an external connection. However the term stream is generic and says nothing about the specific implementation.

Solution 2 - C++

IOStreams are a front-end interface (std::istream, std::ostream) used to define input and output functions. The streams also store formatting options, e.g., the base to use for integer output and hold a std::locale object for all kind of customization. Their most important component is a pointer to a std::streambuf which defines how to access a sequence of characters, e.g., a file, a string, an area on the screen, etc. Specifically for files and strings special stream buffers are provided and classes derived from the stream base classes are provided for easier creation. Describing the entire facilities of the IOStreams library can pretty much fill an entire book: In C++ 2003 about half the library section was devoted to stream related functionality.

Solution 3 - C++

Stream is linear queue that connects a file to the program and maintain the flow of data in both direction. Here the source is any file, I/O device, Hard disk, CD/DVD etc.

Basically stream is if two type 1.Text Stream 2.Binary stream

Text Stream : It is a sequence of character arranges in line and each line terminated by new line (unix).

Binary Stream: It is data as it is coded internally in computer's main memory, without any modification.

Solution 4 - C++

File system is designed to work with a wide variety of devices, including terminals, disk drives, tape drives etc. Even though each device is different, file system transforms each into a logical device called stream. Streams are device independent so same function can be used to write a disk file and a tape file. In more technical term stream provides a abstraction between programmer and actual device being used.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMohamed Ahmed NabilView Question on Stackoverflow
Solution 1 - C++Jonathan WoodView Answer on Stackoverflow
Solution 2 - C++Dietmar KühlView Answer on Stackoverflow
Solution 3 - C++Sandeep_blackView Answer on Stackoverflow
Solution 4 - C++Farsan RashidView Answer on Stackoverflow