*fstream* how to change content inside, or changing lines inside of it.

closed account (1vD3vCM9)
I searched for a solution for SO long, but I DONT find one!
can someone please help me??
how do I rename a line, or change a line in files??
thanks.
closed account (1vD3vCM9)
that's currently the most important thing to my program..
1. Open the file for input, read the lines in the file into a vector; close the file.
2. Modify the contents of the vector.
3. Open the file for output, write the contents of the vector into the file; close the file.
closed account (1vD3vCM9)
really?,
can you please send me an example?, I'm not good at vectors..
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

int main ()
{
    const char* const input_file_name = __FILE__ ; // put the name of the file here
    const char* const output_file_name = "output.txt" ; // put the name of the file here
    // to write back into the same file, use the same file name for both input and output

    std::vector<std::string> all_the_lines ;

    {
        // Open the file for input, read the lines in the file into a vector; close the file.
        std::ifstream file(input_file_name) ;
        std::string line ;
        while( std::getline( file, line ) ) all_the_lines.push_back(line) ;
    }

    {
        // Modify the contents of the vector
        for( std::string& line : all_the_lines ) std::reverse( std::begin(line), std::end(line) ) ;
    }

    {
        // Open the file for output, write the contents of the vector into the file; close the file.
        std::ofstream file(output_file_name) ;
        for( const std::string& line : all_the_lines ) file << line << '\n' ;
    }

    {
        // to verify that the lines have been modified, dump the contents of the output file
        std::cout << std::ifstream(output_file_name).rdbuf() ;
    }
}

http://coliru.stacked-crooked.com/a/201e00ba661c880b
closed account (1vD3vCM9)
Thanks Mate
Topic archived. No new replies allowed.