In article <11**********************@m79g2000cwm.googlegroups .com>
<sh********@gmail.comwrote:
>Thanks for ur input.
Ur-input? That must be some sort of ancient prototype input
(see <http://dictionary.reference.com/search?q=ur>).
>But there is a problem with using "rb+" mode.
If the file does not exist then I want it to be created and "rb+" will
not create a file.
How do i achieve this?
There is the "portable method", and then there is the "good method".
The portable method is to use "w+". As you have seen, this wipes
out the existing file. So use it only if a first attempt with "r+"
fails.
This method is not "good" because there are all kinds of reasons
for "r+" to fail other than "file simply did not exist". Alas,
this is all you get in portable C. The "good" method -- along with
"how much better it is than the portable method" -- generally varies
from one system to another. This means that if you write code to
use this "better way", it will only work on a few systems, instead
of every hosted system.
It is up to you to decide whether the "good" method, whatever that
may be, is more important than the portability you lose by using
it.
(It would be nice if Standard C had a way to say "open file for
reading and writing, creating file if needed but not destroying
existing data if file already exists". But it does not, and
trying "r+" then "w+" is all we get in Standard C.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.