Open the file in append
mode not in write
mode.
fptr=fopen("program.bin","wb");
this statement opens the file program.bin
in binary
write
mode. This mode means everytime the file is opened, the initial contents of file will be truncated.
You should use:
fptr=fopen("program.bin","ab");
to open file in append
mode.
For more details visit this link