I am using fopen to write to a binary file and using the cstdio (stdio.h) library due to legacy code and it must be cross-platform compatible with Windows and Linux.
For the prototype, FILE * fopen ( const char * filename, const char * mode );, I am using const char * mode = "ab", which will append to a binary file. Writing operations append data at the end of the file. The file is created if it does not exist.
I have N number of input files where I process data from and write to one output file for each type, where I have M types. I process one input file and write the data to each corresponding output file. I then will close that ith input file and open (i + 1)th, and repeat the process by appending the data from the input file to the output files.
If an output file exists at the beginning on the executable, I want it deleted. If it exists and I don’t delete it, then when I use the "wb" mode, then it will just append data to the output file, which will result in duplication I don’t want. I am open to a boost solution and I like to maintain standards best as possible (i.e avoid POSIX if possible)
Here is one way
The “w” mode should overwrite the file. It is “a” mode that will avoid deleting a file that already exists.
EDIT: You can also
remove (const char * filename)if you want to delete the files at the beginning of execution. If that’s the case then you never have to use the “wb” mode.