Read txt file line by line, and then upside down the lines order

Hi, Im having a hard time to read and write the txt files.
For example, my original text file is like this:
Line 1
Line 2
Line 3

And then I want to read it and then write the lines into another file to be like:
Line 3
Line 2
Line 1

Any clue would be very appreciated!! Thanks lot!
What part are you having troubles with? The reading, the writing or the order reversal?
There are many ways to read and write files, here's two ways in one: reading with getline and writing with an iterator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
int main()
{
    std::ifstream in("in.txt");
    std::vector<std::string> data;
    for(std::string line; getline(in, line); )
        data.push_back(line);

    std::ofstream out("out.txt");
    copy(data.rbegin(), data.rend(), std::ostream_iterator<std::string>(out, "\n"));
}
@hanst99, the order reversal part
well... just... output it in the opposite order? I don't know what kind of data structure you're saving the results in, but that shouldn't be too problematic. Unless you aren't saving the lines anywhere, then of course it doesn't work (unless you're using recursion).
@hanst99 thanks!
@Cubbi, ur code rocked!! thanks a lot!!!
@Cubbi, it worked perfectly! :) thanks!
Topic archived. No new replies allowed.