Besides binary mode, you need to open it in out
and in
modes. This corresponds to the fopen
mode "r+b"
which opens a file for reading and writing, and doesn't truncate the file if it exist (in fact, it must exist or you will fail to open the file).
References: