C++ Input Output Library

closed account (ypfz3TCk)
I have come to a question in C++ Primer (p 314 , exercise 8.1) that is not clear to me.
"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."

Breaking this down, the function has to do three things:

1. Read a stream until it hits end-of-file
So the >> operator reads input from an istream object - cin.
This stream's end of file can be interrogated by
cin.eof(). This returns true if the end of file bit is set which can be tested with a bool variable
1
2
3
4
5
6
 bool on = false;
on = cin.eof();
if(on == true)
// end of file is reached, else
if(on ==false)
// keep reading cin 

I don't believe that this is completely correct so can someone show me how this code should be presented?

2. Print what is read to the standard output
I can only imagine this to be cout << ? But am lost from here

3. Reset the stream so it is valid before returning the stream
This section of the problem again defeats me.

Can anyone help with this function?
You're over thinking it, just read a line from the istream (use a std::string and std::getline)and output it to stdout (cout). Do that in a loop until you cant read anymore.

3. Reset the stream so it is valid before returning the stream
std::istream has a clear member function, I believe that should reset the stream.
closed account (ypfz3TCk)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<istream>
#include<ostream>

istream& some_function (istream& source)
{
while(source)
{
std::cout << source;
if(source.eof( ) )
{
source.clear( );
return source;
}
else
{ // process}
}
}


This is the solution in case anyone needs it. Thanks for reply.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

// Write a function that takes and returns an istream&.
std::istream& echo_1( std::istream& stm )
{
    auto old_pos = stm.tellg() ; // save the get position on entry

    // The function should read the stream until it hits end-of-file.
    char c ;
    while( stm.get(c) )
    {
        // The function should print what it reads to the standard output.
        std::cout.put(c) ;
    }

    // Reset the stream so it is valid before returning the stream
    stm.clear() ; // clear the error state
    stm.seekg(old_pos) ; // reset the get position

    return stm ;
}

std::istream& echo_2( std::istream& stm )
{
    auto old_pos = stm.tellg() ;

    char c ;
    stm >> std::noskipws ;
    while( stm >> c ) std::cout << c ;

    stm.clear() ;
    stm.seekg(old_pos) ;

    return stm ;
}

std::istream& echo_3( std::istream& stm )
{
    auto old_pos = stm.tellg() ;

    std::cout << stm.rdbuf() ;

    stm.clear() ;
    stm.seekg(old_pos) ;

    return stm ;
}

closed account (ypfz3TCk)
LJBorges - thank you for this solution code
Topic archived. No new replies allowed.