CJCoding With Joseph
15free / daySign inCreate account

Check Open Before Writing

Write a function bool saveText(const string& filename, const string& text) that opens filename with an ofstream (the starter does this as out). If the stream failed to open, return false without writing anything. Otherwise write text followed by a newline and return true. Your function prints nothing itself.

The test prints saved when your function returns true and failed when it returns false. In the success case it then reopens the file and prints every line, so for saveText("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 as missing_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
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.