Which fopen() mode opens a file for both reading and writing, creating it if needed and truncating it if it already exists?
Correct answer C. "w+"
Explanation
"w+" creates the file if needed and truncates it to zero length, then allows both reading and writing. "r+" also allows both but requires the file to exist and keeps its contents, while "a+" keeps contents and sends writes to the end. "rw" is not a valid standard mode. When switching between reading and writing on such a stream, call fflush or fseek in between.
Answer checked How we write and check questions
Discussion