Hi, im just beginning c++ and been reading regarding cin and cout. After reading the chapter, I just got curious what acually a stream is. It is said in some as a series of characters. In a site:

"A stream is an abstraction that represents a device on which input and ouput operations are performed. A stream can basically be represented as a source or destination of characters of indefinite length."

Represents a device? Does that mean a stream can come from or is an abstraction if an IO device (mouse, trackball, printer, etc). Please help me get this cleared up.

Recommended Answers

All 7 Replies

Printer - yes. Mouse/trackball, not so much. A stream is the source or destination of a series of data, either characters, or in the case of binary files, a sequence of bytes that represent memory content. Since it's an abstraction, you can operate on any stream in the same manner - it doesn't matter if you're reading characters from a console (keyboard) with cin or from a file with an ifstream object.

Ok, What you want is a bit informal explanation.
Think of stream as a virtual device (presumably a printer). You are give functions to read/write to that device. You actually don't differentiate if that device is actually your monitor, your printer, the hard disk, or the RAM. What you know is that if you have to write or read any of these devices, you can use the same set of functions to do so.
In C++, there are filestream (which can be assume to be a device as hard disk), memorystream( which can be assume to be a device as your RAM), iostream (which can be assume to be a device as monitor for printing and k/b for reading).

You can use the same >> and << operators to read/write from a file, memory, monitor/keyboard, without knowing the specification of each. Thus you save remembering exclusive function for each of these devices.

vmanes, siddhant3s:

Thanks so much for the explanation. That really cleared it up (especially the abstraction part). The only thing that is left unclear to me is the mouse/trackball part.

It is an input device so why is it not part of iostream (or any stream)?

Thank you very much for the replies.

Tell me something: Can you read words or text from mouse? no.
Well this was very vague conclusion though,
Mouse streams are possible. But, mouse is an optional component of a computer. C++ trace back to those years computer when monitors were new technology!!
So, of course you can read mouse (the location of its pointer) and write mouse (set the position of pointer) but that would be a OS specific issue. Each OS will give different control over mouse. Hence they are not part of standard streams. They are not shipped with every compiler.

Note that when you are well verse in C++, you would be able to create your own mouse stream on WIndows( or perhaps Linux)!!

Well, all I can say is if that stream will be out in the program you cannot process you're program completely, so if I were you just follow the syntax. :D :D

Thank you all for the responses. It's clear now especially siddhant3s' explanation. Thanks again!

A stream is an abstraction that either produces or consumes information.It is linked to a physical device by java I/O system.An input stream can abstract many different kinds of input:from a disc file,keyboard or network socket.in the same way output stream refers to a console.Streams are a clean way to deal with input/output without having every part of your code understand the difference between keyboard and a network

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.