What generally happens when an existing file is opened using ofstream with the default output mode?

#include <fstream>
using namespace std;
int main() {
    ofstream file("data.txt");
    file << "Hello";
    file.close();
    return 0;
}
                        

Options

  • A. The text is always appended to the end
  • B. The existing contents are normally truncated
  • C. The file is opened for reading only
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. The existing contents are normally truncated

Detailed Explanation

Correct Answer: Option B

Explanation: Opening an existing file with ofstream in its default output mode normally truncates the file. The program writes Hello into the newly emptied file. If the file does not exist, it is normally created.