Interview Questions

I am trying to update a file in place ...

C Interview Questions and Answers


(Continued from previous question...)

I am trying to update a file in place ...

Q: I'm trying to update a file in place, by using fopen mode "r+", reading a certain string, and writing back a modified string, but it's not working.

A: Be sure to call fseek before you write, both to seek back to the beginning of the string you're trying to overwrite, and because an fseek or fflush is always required between reading and writing in the read/write "+" modes. Also, remember that you can only overwrite characters with the same number of replacement characters; there is no way to insert or delete characters in place. Finally, remember that overwriting in text mode may truncate the file at that point, and that you may have to preserve line lengths.

(Continued on next question...)

Other Interview Questions