← Back to Streams
Question 665
Reverse Lines in Place
Reverse Lines in Place
Write a functionvoid 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 withgetlineinto avector<string>, then reopen the file inios::out | ios::truncmode 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 linesa,b,cit 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
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.