Which of the following function is used to open a file in C++?
a) open()
b) fopen()
c) ofstream
d) ifstream
Answer:
b) fopen()
Explanation:
The fopen()
function is used to open a file in C. In C++, file handling is typically done using classes like ifstream
for input file streams, and ofstream
for output file streams. These classes are more powerful and flexible than the C-style fopen()
function, as they support object-oriented file handling.
Understanding the differences between C and C++ file handling methods is crucial for writing programs that are efficient and maintainable in either language.