1. Home
  2. Computer Science MCQs
  3. C Programming
  4. What does fopen("data.txt", "w") do when data.txt already…

Added 30 Nov -0001

What does fopen("data.txt", "w") do when data.txt already exists?

  1. Opens it read-only until data is written
  2. Truncates the file to zero length before writing Correct answer
  3. Appends new text after the existing content of the file
  4. Fails and returns NULL to protect the file

Correct answer

B. Truncates the file to zero length before writing

15 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs