954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

What is a stream exactly?

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.

gnobber
Newbie Poster
14 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

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.

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

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.

gnobber
Newbie Poster
14 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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)!!

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

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

certifmix123
Newbie Poster
10 posts since Apr 2009
Reputation Points: -1
Solved Threads: 1
 

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

gnobber
Newbie Poster
14 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

sai sushma
Newbie Poster
1 post since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You