I have a file that I'm writing data to, using the functions in cstdio
. I want to make sure the file has successfully been written to file without any interruptions, so I know what to expect from the file when I later read from it. The way I aim to do that, is by first writing 8 bytes worth of zeroed out data to the very beginning of the file, writing the file as normal, and then as a last step, rewinding the file and overwriting those first 8 bytes with a specific signature to denote that the file has been completed and well-formed.
My question is, what is the best way to edit those first 8 bytes in place?
With fopen()
, what would be the appropriate opening mode (the second argument) to use?
Is it safe to just rewind()
to the beginning and use fwrite()
, or is there a specific function special to the act of overwriting?
WhozCraig is right. Using
wb+
is completely fine as well asrewind
is.You can also take a look at file locking, which ensures that: