Check Open Before Writing
easyQuestion 601Write 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, so print one after the last line.
Your output
Run your code to see the output here...