CJCoding With Joseph
15free / daySign inCreate account

Reverse Lines in Place

Write a function void reverseLines(const string& filename) that reverses the order of the lines in a text file: the last line becomes the first, and so on.

Read every line with getline into a vector<string>, then reopen the file in ios::out | ios::trunc mode and write the lines back from last to first, each followed by a newline. A file with a single line stays the same.

The test creates the file, calls your function and prints every line of the file. For a file with the lines a, b, c it prints the output shown in Expected Output.

Do NOT write a main function.

Expected Output:

c
b
a
only one

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Streamsfstream
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.