N
No Such Luck
I have the need to open an arbitrary file and perform random access
reads, writes and seeks. The file may or may not exist. If it doesn't
exists, it needs to be created. I'm having a hard time trying to
figure out which mode the file should be opened in. None of them seem
like a good fit.
rb+ and r+b modes won't create the file if it doesn't exist
wb+ and w+b will overwrite the file if it does exist.
I think a possible solution would be to try and open the file in rb+
mode, and if that fails, open it in wb+ mode, creating it. However, I
feel it is sort of risky doing this in 2 steps. If something (i.e.,
network hiccup, disk issue, etc) was responsible for the initial rb+
failure, coincidental race timing could see me eradicating a perfectly
good file on accident.
The errno offers little help here, too. The same errno is reported
whether the destination is unreachable, or the destination is
reachable but the file just isn't there.
Any advice on the best way to handle this?
Thanks in advance...
reads, writes and seeks. The file may or may not exist. If it doesn't
exists, it needs to be created. I'm having a hard time trying to
figure out which mode the file should be opened in. None of them seem
like a good fit.
rb+ and r+b modes won't create the file if it doesn't exist
wb+ and w+b will overwrite the file if it does exist.
I think a possible solution would be to try and open the file in rb+
mode, and if that fails, open it in wb+ mode, creating it. However, I
feel it is sort of risky doing this in 2 steps. If something (i.e.,
network hiccup, disk issue, etc) was responsible for the initial rb+
failure, coincidental race timing could see me eradicating a perfectly
good file on accident.
The errno offers little help here, too. The same errno is reported
whether the destination is unreachable, or the destination is
reachable but the file just isn't there.
Any advice on the best way to handle this?
Thanks in advance...