Writting in a saved File.

Open the file in append mode. Run this program a few times and observe its output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    const std::string file_name = "my_file.txt" ;

    {
        std::ofstream file( file_name, std::ios::app ) ; // open the output file in append mode

        // accept a line from the user and append it to the file
        std::string line ;
        std::getline( std::cin, line ) ;
        file << line << '\n' ;
    }
    {
        // for debugging: dump the contents of the (modified) file
        std::cout << "\n\nfile now contains:\n---------------\n" << std::ifstream(file_name).rdbuf() ;
    }
}
Topic archived. No new replies allowed.