← Back to Streams
Question 601
Check Open Before Writing
Check Open Before Writing
Write a functionbool saveText(const string& filename, const string& text)that opensfilenamewith anofstream(the starter does this asout). If the stream failed to open, returnfalsewithout writing anything. Otherwise writetextfollowed by a newline and returntrue. Your function prints nothing itself. The test printssavedwhen your function returns true andfailedwhen it returns false. In the success case it then reopens the file and prints every line, so forsaveText("notes_a.txt", "keep this")the output is exactly what is shown in Expected Output. For a path inside a folder that does not exist, such asmissing_dir/notes.txt, the output is just: failed Do NOT write a main function.
Expected Output:
saved keep this failed
= 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.