What is the use of ios::trunc mode?
2023
What is the use of ios::trunc mode?
- A.
To open a file in input mode
- B.
To open a file in output mode
- C.
To truncate an existing file to zero
- D.
More than one of the above
- E.
None of the above
Attempted by 104 students.
Show answer & explanation
Correct answer: C
ios::trunc mode is used when opening a file for output. It ensures that if the file already exists, its contents are truncated to zero length, effectively erasing all previous data. This mode is typically used with ofstream or fstream in C++ to start writing from a clean file.