ate and app in files in c++

closed account (1vf9z8AR)
Can you please give me sort of exmaple program of app and ate?The ones on the net are too complicated and mixed with other programs.
I mean this ios::ate and ios::app in files.
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
48
49
50
51
#include <iostream>
#include <fstream>
#include <string>

void create_test_file( const std::string& path )
{
    std::ofstream(path) // create if file does not exist, truncate if it exists
         << "   line 1\n   line 2\n   line 3\n" ;
}

void debug_dump_file( const std::string& path )
{
    std::cout << std::ifstream(path).rdbuf()
              << "\n-----------------------------------------------------\n" ;
}

int main()
{
    const std::string test_file = "/tmp/test.txt" ;

    create_test_file(test_file) ;
    debug_dump_file(test_file) ;

    {
        // open the file for output and seek to the end of the file
        // note: std::ios::in - open an existing file, do not truncate it
        std::ofstream file( test_file, std::ios::in|std::ios::ate ) ;
        file << "## line 4\n" ; // writes at the end of the file

        file.seekp(0) ; // seek to the beginning
        file << "## line 5\n" ; // writes at the beginning of the file
                             // (avances the put position by the number of characters written)

        file << "## line 6\n" ; // writes at the current put position
    }
    debug_dump_file(test_file) ;

    {
        // open the file for output; all output is appended to the end of the file
        // if the file exists, do not truncate it
        std::ofstream file( test_file, std::ios::app ) ;
        file << "** line 7\n" ; // appends at the end of the file

        file.seekp(0) ; // seek to the beginning
        file << "** line 8\n" ; // always appends at the end of the file

        file.seekp(0) ; // seek to the beginning
        file << "** line 9\n" ; // always appends at the end of the file
    }
    debug_dump_file(test_file) ;
}

http://coliru.stacked-crooked.com/a/2dce102dab4541ea
closed account (1vf9z8AR)
thanks.It really helped me.
Topic archived. No new replies allowed.