Using C, does anyone know how to overwrite a character in a file without copying the whole file to another new one?
Using C, does anyone know how to overwrite a character in a file without copying the whole file to another new one?
Does fprintf overwrite a string?
>>
fopen() the file in "r+" mode
fseek() to the char you want to overwrite
<<
You can't (or rather shouldn't) be fseek'ing on a non-binary stream, unless you are using the return value from a prior call to ftell() to determine the offset. If you do, you may run into trouble and start overwriting the wrong bytes in the file.
So, the short answer is to use mode "r+b", but this will have an affect on any \n characters you output, and may cause trouble on Windows systems (due to CRLF end of line markers).
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
You could read the entire file into a buffer (as text), modify the character(s) you want then save it again. This way you don't have to think of the fseek problem above.
Not the best solution on big files though (the buffer consumes memory).
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.