Quick ifstream & ofstream Question

Hey guys!

So I wrote the proper program my professor wants. Now she needs me to copy that programs' results over to a new data file. Presumably, it should go something like this:

1. cout << "File name: "
2. cin >> The file (ifstream)
3. Have the program run using the data from the ifstream file.
4. Instead of printing the results, copy them over to a new file (ofstream).
5. Now I should be able to run a "cat" or "more" command on the ofstream file and have the results of the program displayed.

How would I go about doing this? Exercise is due tomorrow night.
To make minimal changes to the program (if it has a lot of hard-coded std::cout <<),
redirect the output of std::cout to a file.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>

int main()
{
    std::cout << "1. this would be printed on stdout\n" ;

    {
        // redirect std::cout to file output.txt
        std::filebuf file_buffer ;
        file_buffer.open( "output.txt", std::ios::out ) ; // open for output
        // associate the file buffer with std::cout
        const auto old_cout_buff = std::cout.rdbuf( std::addressof(file_buffer) ) ;

        std::cout << "2. this would be printed to file\n" ;

        std::cout << "3. this too would be printed to file\n" ;

        // restore the original behaviour of std::cout
        std::cout.rdbuf(old_cout_buff) ;
    }

    std::cout << "4. this too would be printed on stdout\n" ;
}

http://coliru.stacked-crooked.com/a/b9695e6bd360af65

Otherwise, create an output file stream: std::ofstream file_out( "output.txt" ) ;
And then send the program output to the file stream: file_out << "what ever"
A very minor add to above, if I may: #include <memory> for std::addressof()
Hmm, I am not sure what "const auto" does, but I copy/pasted that portion of code and it ran an error.
On the second suggestion, I am not allowed to output to filestream. It has to be the way I mentioned above.

Should look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>

  cout << "Enter name of exit file:";
  cin.get (cin, two);
  byebye.open(two);

  for (int i = 0; i < 1; i++)
    {
      cout << one << endl;
    }

  void close();

  return 0;
}

> I am not sure what "const auto" does

See: http://www.stroustrup.com/C++11FAQ.html#auto


> I copy/pasted that portion of code and it ran an error.

This is a C++11 feature, so if a compiler like the GNU compiler which defaults to a non-standard dialect based on legacy C++ is being used, C++11 conformance has to be explicitly demanded.
Compile with the options: -std=c++11 -pedantic-errors -Wall -Wextra

Legacy C++ version:
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
#include <iostream>
#include <fstream>

int main()
{
    std::cout << "1. this would be printed on stdout\n" ;

    {
        // redirect std::cout to file output.txt
        std::filebuf file_buffer ;
        file_buffer.open( "output.txt", std::ios::out ) ; // open for output
        
        // associate the file buffer with std::cout
        // const auto old_cout_buff = std::cout.rdbuf( std::addressof(file_buffer) ) ;
        std::streambuf* const old_cout_buff = std::cout.rdbuf( &file_buffer ) ;

        std::cout << "2. this would be printed to file\n" ;

        std::cout << "3. this too would be printed to file\n" ;

        // restore the original behaviour of std::cout
        std::cout.rdbuf(old_cout_buff) ;
    }

    std::cout << "4. this too would be printed on stdout\n" ;
}

http://coliru.stacked-crooked.com/a/84c26afbd8df29e6
Thanks for the feedback!
Registered users can post here. Sign in or register to post.