Home c++ What is a stream (stream)?

What is a stream (stream)?

Author

Date

Category

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

What is a stream, and what does it represent?


Answer 1, Authority 100%

Stream is just an abstraction. Convenient abstraction to solve problems.

But if it is so easier – consider that this is such a pipe. On the one hand, data is shoved in it, and on the other hand, it goes somewhat (for example, on the console or file). Since the pipe is not zero size, part of the data there may “delay” – this is the so-called buffering. Understanding this can facilitate understanding of some strange behavior of standard streams.

Consider for example, such reading from the console:

int n;
STD :: String S;
STD :: CIN & GT; & GT; n & gt; & gt; s;

and input data:

123a AAA

How will it work? Initially, the standard thread needs to read the number. It will read until there is a number. The n will fall 123. The “A AAA” left. The “A AAA” left. Next you need to read the string. And the string is read before the space or translation of the line. Therefore, only one thing will be read. And many believe that three a.
I brought this example not just like that. Many come across it.

How is it implemented inside? Very simple. There is a queue (that is, if it is very roughly an array), from where they read byte data for byte. If there is nothing in the queue – the library code refers to the functions of the operating system and reads from the console / file / socket) and folds in the queue. Of course, in some cases, this queue can be implemented on the basis of the operating system, and may well. But for the user (that is, the programmer) is transparent.

also works and output.

What are the convenient streams?

After the “connected” stream to the file or console, then everything goes the same. It is possible, for example, write the functions that will be in a stream, but at the same time absolutely not to know where exactly they bring. This allows you to write generalized algorithms and other goodies.

For example, you need to make the function of the formatted output of the number on the console (with specials. wishes). You can certainly shut out the output to STD :: COUT, but how to protest? You can write a function that will be on the entrance to receive Ostream (STD :: COUT ancestor) and display it. Then you can use such a function and output to StringStream, which is easily converted to the string and the result can be output.


Answer 2, Authority 24%

Stream (Stream) is an abstraction that represents a device with input and output operations. Thus, the flow can be understood as a source and / or receiver of an infinite number of characters.

Threads are usually attached to the physical source / symbol receiver, such as a file, keyboard or console. At the same time, the characters read from or recorded into the stream are really physically entered or displayed on the device. For example, file streams in C++ are objects for manipulations with files: After the file stream opens the file, any input / output operation on this thread is physically displayed in the file.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions