← Quiz: File Handling in C++: all 10 questions
Quiz: File Handling in C++ · Question 7 of 10
What does this print? (code)
What does this print?
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
ifstream in("missing_file.txt");
string s = "none";
in >> s;
cout << (in ? "ok" : "fail") << " " << s << "\n";
return 0;
}
- A.ok none
- B.fail none
- C.fail
- D.Runtime crash
Answer & explanation
Correct answer: fail none
Opening failed, so the stream is in a failed state; the read does nothing and s keeps its old value.
Can you get the rest right without peeking?
Take the timed Quiz: File Handling in C++ quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →