1

I often see people using the term stream but I never get exactly what it means. And what does standard mean? just mean input from terminal and output to terminal? How about stderr? When do we need to use it and what effect it has?

Second, can we create our own stream? And why we need to create it?

OneZero
  • 10,918
  • 11
  • 54
  • 88

3 Answers3

2

In type theory, a stream is just an infinite list of data.

However, in C or C++ it is usually thought of as either an infinite source or an infinite sink. Of course, the infinite is actually a lie most of the times, but it is a useful abstraction as it underlines that the size is unknown.

I think the terms source and sink are more useful. You can think of stderr a sink for characters. From the point of view of the program, it is just something that consumes characters without any apparent effect after all.

You can of course create streams (either sources or sinks or both at the same time).

Matthieu M.
  • 268,556
  • 42
  • 412
  • 681
  • I've tried to explain what *stream* means, in my answer [here](http://stackoverflow.com/a/6010930/415784), in order to explain why *copying* of stream doesn't make sense in C++. – Nawaz Jun 05 '12 at 09:22
1

I often see people using the term stream but I never get exactly what it means

C++ provides the following classes to perform output and input of characters to/from files:

ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream : Stream class to both read and write from/to files.

I always consider stream as like file handle we get to perform operations on file.

And what does standard mean?

In C++, the C++ Standard Library is a collection of classes and functions.

The C++ Standard Library provides several everyday functions for tasks such as finding the square root of a number.

Features of the C++ Standard Library are declared within the std.int namespace.

How about stderr?

The standard error stream is the default destination for error messages and other diagnostic warnings. Like stdout, it is usually also directed to the output device of the standard console.

It is also possible to redirect stderr to some other destination from within a program using the freopen function.

Second, can we create our own stream? And why we need to create it?

This will guide you

Vipul
  • 31,716
  • 7
  • 69
  • 86
1

streams are classes derived from std::ios_base. They present elements one after another compared to random access. You create one by calling the appropriate constructor. If you want to implement your own stream, derive from ios_base and implement all methods accordingly.

Tobias Langner
  • 10,402
  • 5
  • 45
  • 73