What is a stream?

In books, they write about standard I / O streams in C++, writing to a stream and reading from a stream.

What is a stream, and what is it?

Author: Abyx, 2015-07-14

2 answers

A stream is just an abstraction. A convenient abstraction for solving problems.

But if it makes it easier, consider it a pipe. On the one hand, data is shoved into it, and on the other hand, it goes somewhere (for example, to the console or to a file). Since the pipe is not of zero size, some of the data there may be "delayed" - this is the so-called buffering. Understanding this can make it easier to understand some of the strange behavior of standard threads.

Consider, for example, such a reading with consoles:

int n;
std::string s;
std::cin >> n >> s;

And such input data:

123a aaa

How will this work? First, the standard stream needs to read the number. It will read as long as there is a number. N will get 123. "A aaa"is left in the "pipe". Then you need to read the line. And the string is read before a space or a line feed. Therefore, only one a will be read. And many believe that three a's will be read. I gave this example for a reason. Many people fall for it.

How is this implemented internally? Very simply. There is a queue (that is, if very roughly - an array), from which data is read byte by byte. If there is nothing in the queue, the library code accesses the operating system functions and reads from the console / file/socket) and adds it to the queue. Of course, in some cases, this queue can be implemented on the basis of the operating system, or it can be explicitly implemented. But for the user (i.e. the programmer) it's transparent.

The output works similarly.

Why are threads convenient?

After after the stream is "connected" to the file or console, everything goes the same way. You can, for example, write functions that will output to the stream, but you absolutely do not know where exactly they output. This allows you to write generalized algorithms and other goodies.

For example, you need to make a function of formatted output of a number to the console (with special requests). You can of course encode the output to std:: cout, but how to test it? You can write a function that will receive ostream as input (ancestor of std:: cout) and output to it. Then you can use such a function and output it to a stringstream, which is easily converted to a string and the result can be verified by a test.

 21
Author: KoVadim, 2015-07-14 16:46:35

A stream is an abstraction that represents a device with input and output operations. Thus, a stream can be understood as a source and / or receiver of an infinite number of characters.

Streams are usually bound to a physical character source / sink, such as a file, keyboard, or console. In this case, the characters read from or written to the stream are actually physically entered or output to the device. For example, file streams in C++ are objects for File manipulation: After a file stream opens a file, any I/O operation on that stream is physically displayed in the file.

 5
Author: cpp_user, 2015-07-14 19:42:33