Hello, I am trying to get my head around the concept of streams. I have a hard time grasping the concept of a lot of things, so I try and learn stuff the most simplest of ways. So I am hoping someone can clarify if I have this correct or not. Is a stream basically just the information being passed around, and nothing else. For instance. If I was passing in a name from keyboard into a console program the stream would just be the characters of the name passed in, and not the buffer that the name is stored in?
I like the cin as your best example to kind of get a feel for it. Cin represents the user typing data at the keyboard. You read it, and its gone** is one common theme to a stream, and how much they typed/unknown quantity is the second theme.
** sort of. some streams are more friendly to rewinds /backing up than others. It is useful to go backwards sometimes, or restart, and we implement the tools to allow it. A file stream can be backed up easily, cin can only be backed up by keeping the data and putting it back on the buffer.
in data structure terms, they are 'simple queues'. data is stuffed into it, reading it removes it forever, and you don't normally know how much is in it you just read it until its empty. From that most basic idea, there are a number of variations, but that is the most oversimplified high level explain I can manage today.
For your question: the letters are the stream's data, but the buffer "is" the stream. Streams can be implemented all kinds of ways, its a concept, not a 'thing'. There will always be some sort of container down in there holding the data. It could be a tree or linked list even, does not really matter.
Uh OK, I dont think I fully understand yet, but may be getting there. I did see that link before but I failed to understand it.
What I am thinking now, is, since Jonnin said that for the cin example the buffer is the stream, if, for example, I was writing to file, would the file in that example be the stream? And if i was reading from a file and putting the data into a list, would the list be the stream?
I was writing to file, would the file in that example be the stream?
No the file is the destination of the stream.
And if i was reading from a file and putting the data into a list, would the list be the stream?
No the file is the source of the stream the list would, in this instance, be the destination of the stream. The data is "flowing" along the stream from source to destination.
Normally a stream is considered the connection. And the stream is considered an abstraction. For example cin is an abstraction of the act of inputting some value into some variable.
the buffer is part of the *implementation* of the stream. Conceptually, the stream is everything from the keyboard's internal hardware buffer to the operating system or driver program's buffer to the c++ program's stream object and whatever is in that and so on.
you can point to cin and say "there it is! That is the stream from the keyboard". Or in the case of the buffer(s) used by cin, you can point to those and say "I see it, I see that stream" but like a river in real life, while you can look at it from the ground and say you see it, you are only looking at a relatively small piece of it from that viewpoint.
I have a somewhat different interpretation of streams. istream and ostream are classes that provide formatted input and output. They can do unformatted I/O too, but their main purpose is to read or write formatted text.
The stream is not the buffer! A stream contains a pointer to an object called a streambuf that provides the actual data bytes through a bunch of virtual methods. You can get or change the streambuf using the inherited rdbuf() method. At work we have a streambuf class that sends its data to a debug server. Sometimes we set cout's streambuf to one of these. Poof! cout now goes to our debug server.
File streams (ofstream and ifstream) are derived from istream and ostream to provide open() and close() methods (and others I supposed). Their streambuf interacts with a file.
stringstreams are derived from istream and ostream to provide the str() function to get & set the underlying data. They use a streambuf that interacts with a memory buffer, perhaps a std::string, but that isn't required.
A stream reads & writes formatted data. A streambuf reads, writes and (possibly) buffers bytes. That's the way it's implemented and I think it's a good way to interpret it.