Truncate or Append
mediumQuestion 616Write a function void save(const string& filename, const string& text, bool append) that writes text followed by a newline to filename. When append is false the file is truncated first; when append is true the file is opened in append mode (ios::app).
Test 1 starts with a file containing the line old, then calls save(filename, "first", false) and then save(filename, "second", true), and prints every line of the file (see Expected Output).
Test 2 starts with a file containing the line kept and calls save(filename, "added", true):
kept
added
Do NOT write a main function.
Expected output
first second kept added
= newline. The output ends with a newline, so print one after the last line.
Hints4 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.