CJCoding With Joseph
15free / daySign inCreate account

Truncate or Append

Write 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 — print one after the last line.

Hints4 available
Topics:
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.