Thread: What is a stream? What really is a stream object?

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    9

    What is a stream? What really is a stream object?

    Hi,

    My definition of a stream is a sequence of data, but it does not really make sense when thinking about stream objects.

    For example, "cin >> var;" awaits for the user to input something; and after the user presses enter (after inputting something) the input is in the buffer, where all or part of data is to be extracted to "var". Is the buffer equivalent to the cin object? What is the role of cin object? Thinking something as cin represents a stream or the input device confuses me.

    What's the model behind input and output? Can somebody provide an analogy to help me understand the model?

    I appreciate your help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ramcin Oudishu
    My definition of a stream is a sequence of data, but it does not really make sense when thinking about stream objects.
    We could first look at what the C++ standard has to say about I/O streams, e.g.,
    Quote Originally Posted by C++11 Clause 27.4.1 Paragraph 1
    The header <iostream> declares objects that associate objects with the standard C streams provided for by the functions declared in <cstdio> (27.9.2), and includes all the headers necessary to use these objects.
    Now concerning streams in C:
    Quote Originally Posted by C99 Clause 7.19.2 Paragraphs 1-3 (extract)
    Input and output, whether to or from physical devices such as terminals and tape drives, or whether to or from files supported on structured storage devices, are mapped into logical data streams, whose properties are more uniform than their various inputs and outputs. Two forms of mapping are supported, for text streams and for binary streams.

    A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character.

    A binary stream is an ordered sequence of characters that can transparently record internal data.
    Quote Originally Posted by Ramcin Oudishu
    Is the buffer equivalent to the cin object? What is the role of cin object?
    The standard input buffer (or rather standard input: I'm not sure if a buffer is actually guaranteed) is mapped to the C++ notion of the standard input stream, which is represented by the std::cin object. So the idea here is to imagine that standard input is a stream of characters, and then to read from that stream, you use the std::cin object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Can somebody provide an analogy to help me understand the model?
    O_o

    I'm not sure a good analogy actually exists.

    The C,C++, and POSIX notion of streams, even files/sockets/pipes, is an extreme abstraction.

    The streams may be buffered. The streams may not be buffered.
    The streams may be navigable--may "seek"/"tell" position. The streams may not be navigable.
    The streams may preserve order. (You may create a compliant UDP stream, for example.) The streams may not preserve order.
    The streams may acknowledge "external" errors. (You may create a compliant TCP stream which does/does not set EOF/error/fail bits if the client/server connection terminates, for example.) The streams may not acknowledge "external" errors.

    I could continue noting other possibilities, but the point would remain the same.

    All the possibilities makes a good analogy difficult.

    I'd advise you simply think of streams as they are: an interface for reading/writing some sort of data.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    A stream is a serial interface to data. That is, you can only read or write the data in a particular order. You can sometimes move the position you're accessing, but you can't have multiple locations accessed on the same stream at once.

    cin is not an input device or a buffer, although it uses both. It's an object that lets you read from data from a standardized text source.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-25-2012, 08:56 AM
  2. std::string object is overwritten in output stream
    By eatwithaspork in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2009, 03:20 PM
  3. stream object
    By bp316 in forum C++ Programming
    Replies: 2
    Last Post: 01-23-2009, 03:28 AM
  4. Testing a stream object in a while statement
    By sudar in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2007, 08:50 PM
  5. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM

Tags for this Thread