Reverse Lines in Place
mediumQuestion 665Write 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, so print one after the last line.
Hints4 available
Sign in to use hintsTopics
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.