← Back to Streams
Question 605
Overwrite an Existing File
Overwrite an Existing File
Write a functionvoid overwrite(const string& filename, const string& text)that replaces whatever the file currently contains with a single line:textfollowed by a newline. Use the default open mode ofofstream. The test first writes the lineOLDinto the file, callsoverwrite(filename, "NEW"), then reopens the file and prints every line, so the output is exactly what is shown in Expected Output. Do NOT write a main function.
Expected Output:
NEW fresh
= 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.