← Back to Streams
Question 602
Append a Line
Append a Line
Write a functionvoid appendLine(const string& filename, const string& line)that opensfilenamein append mode (ios::app) and writeslinefollowed by a newline at the end of the file, keeping the existing contents. Each call adds exactly one line. The test creates the file containing the single linestart, callsappendLinetwice with"second"and"third", then reopens the file and prints every line (see Expected Output). Do NOT write a main function.
Expected Output:
start second third first last
= newline. The output ends with a newline — print one after the last line.
Hints2 available
Sign in to use hintsTopics:
Streamsofstream
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.