What is the likely result of this code if data.txt does not exist?
fstream file("data.txt", ios::in | ios::out);
if (file.is_open())
cout << "Opened";
else
cout << "Failed";
Options
-
A.
It prints Opened and creates the file
-
B.
It prints Failed because opening generally fails
-
C.
It always prints both messages
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
B.
It prints Failed because opening generally fails
Detailed Explanation
Correct Answer: Option BExplanation:Opening an existing file with
ios::in | ios::out requests reading and writing. If the file does not exist, this combination generally does not create it, so
is_open() returns false and the program prints
Failed.