"Write a function that takes and returns an istream&. The function should read the stream until it hits end-of-file. The function should print what it reads to the standard output. Reset the stream so that it is valid before returning the stream."
"Test your function by calling it, passing cin as an argument."
It is almost impossible to get an eof bit set in standard input stream using stram operations (I think it is only possible if you direct a file into input stream or press Ctrl-Z (Ctrl-D on linux AFAIR) in console)
Just read a little bit on rdbuf() in the reference section here, so it's a pointer to a stream's buffer? I understand the stream buffer as a pipe that's holding data that's suppose to be passed to some kind of object/variable. Am I close?
So cout << is.rdbuf() is actually reading the data in the pipes (stream buffer) inputted from the call of stream(cin)? If that's the case:
"Use the function to print the contents of an istringstream object."
@MiiNiPaa yeah I thought of letting the program run for awhile until it stops to see what happens, then pretty much figured it's stupid after 5 mins in and knew I was wrong.
Yes. the streambuf object is the buffer and rdbuf() returns a pointer to it.
> I understand the stream buffer as a pipe that's holding data
A stream buffer holds a sequence of characters in memory; an array of characters which is a subsequence of of the characters being read or written. And the stream buffer refreshes this array as required - reading more characters in when it is empty (input) or writing the characters out when it is full (output).
Though these two requirements taken together are somewhat incongruous :
1. "Reset the stream so that it is valid before returning the stream."
2. "Test your function by calling it, passing cin as an argument."
There is no guaranteed, portable way to reset stdin once it reaches eof.
Thanks JLBorges for the explanation and links. I'll take this time to read and digest them. Appreciate the warning, but I guess the authors thought that would be more on the advanced side of things.