53

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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Mohamed Ahmed Nabil
  • 3,389
  • 8
  • 32
  • 48
  • As someone who didn't understand this either at some point, now I've added an answer here: https://stackoverflow.com/a/52936291/3933131 hoping to help some folks. Please note that it's mostly explained in C, that's why I haven't added it here, however, I think you can still get a good idea of it if you read it carefully. – M-J Oct 23 '18 at 02:15

4 Answers4

55

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.

Jonathan Wood
  • 59,750
  • 65
  • 229
  • 380
  • 4
    So it is like a "river stream" that flows from source to destination. And the stream is the river itself – Mohamed Ahmed Nabil Aug 27 '12 at 16:03
  • 1
    So i can consider a stream as the thing that brings (for example) data from the console to the variabel – Mohamed Ahmed Nabil Aug 27 '12 at 16:06
  • If it is simply something like that, How can it have a newline charecter stuck in it? – Mohamed Ahmed Nabil Aug 27 '12 at 16:10
  • 1
    What is "stuck in it"? There is nothing about the general concept of streams that defines what kind of data can be transferred or how you transfer it. If you mean stuck as in it won't flow with the stream, then that sounds like a bug. I don't have enough information to say. – Jonathan Wood Aug 27 '12 at 16:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15865/discussion-between-mohamed-ahmed-nabil-and-jonathan-wood) – Mohamed Ahmed Nabil Aug 27 '12 at 16:17
  • 3
    I think many people who teach have found that technical details are best explained in technical terms. Metaphors aren't always suitable and may well encourage erroneous conclusions. – Kerrek SB Aug 27 '12 at 16:22
  • 3
    @KerrekSB: The term "stream" is itself a metaphor. So explaining the metaphor seems apt in this case. – Jonathan Wood Jul 16 '15 at 16:26
  • I think the abstraction of stream here is self-explanatory from its word, your answer isn't very helpful! – el psy Congroo May 19 '17 at 11:00
11

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.

Dietmar Kühl
  • 141,209
  • 12
  • 196
  • 356
8

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.

Sandeep_black
  • 1,004
  • 12
  • 16
4

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.

Farsan Rashid
  • 1,309
  • 2
  • 14
  • 25