What does the following C++ program do to an existing file named data.txt?

#include <fstream>
using namespace std;
int main() {
    ofstream file("data.txt", ios::out | ios::trunc);
    file.close();
    return 0;
}
                        

Options

  • A. It preserves all existing contents
  • B. It deletes the file permanently
  • C. It opens the file for reading only
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

E. None of the above

Detailed Explanation

Correct Answer: Option E — None of the above

Explanation: If opening succeeds and data.txt already exists, ios::out | ios::trunc opens it for output and truncates its contents to zero length. The file itself is not permanently deleted. If it does not exist, it is normally created.