← Back to Streams
Question 616
Truncate or Append
Truncate or Append
Write a functionvoid save(const string& filename, const string& text, bool append)that writestextfollowed by a newline tofilename. Whenappendis false the file is truncated first; whenappendis true the file is opened in append mode (ios::app). Test 1 starts with a file containing the lineold, then callssave(filename, "first", false)and thensave(filename, "second", true), and prints every line of the file (see Expected Output). Test 2 starts with a file containing the linekeptand callssave(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
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.