← Back to Streams
Question 576
Did the File Open?
Did the File Open?
Write a functionbool canOpen(const string& filename)that tries to open the file for reading and returnstrueif it opened successfully andfalseotherwise. The starter already creates theifstream in; return the result of itsis_open()member. The test printsopenedwhen your function returnstrueandfailedwhen it returnsfalse. The first test passes a file that exists; the second passes the pathmissing_dir/notes.txt, which cannot be opened. The two tests print, respectively (see Expected Output). Do NOT write a main function.
Expected Output:
opened failed
= newline. The output ends with a newline — print one after the last line.
Hints2 available
Sign in to use hintsTopics:
Streamsifstream
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.