What does fopen("data.txt", "w") do when data.txt already exists?
Correct answer
B. Truncates the file to zero length before writing
15 views
B. Truncates the file to zero length before writing
15 views
The "w" mode opens a file for writing and truncates any existing content, so previous data is lost as soon as the open succeeds. The "a" mode appends instead and creates the file if it is missing, while "r+" opens for both reading and writing without truncation. Forgetting this difference is a classic cause of accidentally erased log files.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.