I'm attempting to open a file which may or may not exist for read and write access. I'll also need to perform seek operations on this file. The issue I'm having is that the "r" file flag requires that the file exist, the "w" flag discards the existing contents, and the "a" flag disables seek operations by always appending to the end of the file. So none of the three options seem to do what I need. Any thoughts on a workaround? Or will I need to optionally create the file and then re-open it with "r+" flags?
I'm using cstdio within c++ (fopen, fseek, rewind, etc). Profiling has shown iostreams to be too slow for my application.
Not possible to achieve it with single fopen call, since none of the modes available do what you want. I think that creating a file if it does not exist and then using r+ is the best option.