What happens when an input file stream attempts to open a nonexistent file using ios::in?

#include <fstream>
#include <iostream>
using namespace std;
int main() {
    ifstream file("missing.txt", ios::in);
    cout << file.is_open();
    return 0;
}
                        

Options

  • A. It prints 1 in all cases
  • B. It prints 0 if the file cannot be opened
  • C. It automatically creates the missing file
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. It prints 0 if the file cannot be opened

Detailed Explanation

Correct Answer: Option B

Explanation: If missing.txt does not exist or cannot be opened, the input stream fails to open and is_open() returns false, which is printed as 0. Opening an input file does not normally create a missing file.