D
Dave O'Hearn
I want to open a file for both reading and writing, but when I do
"ios::in|ios:ut" with an fstream, it creates the file if it doesn't
already exist. I don't like that last part. C's fopen has the "r+"
mode, where the file will open for reading "+ also writing", but it
will not create a new file if one isn't already there; it fails with
an error. POSIX also has this behavior with it's 'open' call, you just
leave the 'create' bit out, and you can open for read/write without
creating.
But I can't figure out how to do this with iostreams. The only
ios:penmode values I know of are: app, ate, binary, in, out,
trunc... and none of them have to do with create-if-doesn't-exist. So
far as I can tell, with iostreams, that is implicit in ios:ut,
whether you want it or not.
Is there a way, or do I have to open the file for reading to check
that it exists, then close it and reopen it for read/write?
Thanks in advance for any advice.
"ios::in|ios:ut" with an fstream, it creates the file if it doesn't
already exist. I don't like that last part. C's fopen has the "r+"
mode, where the file will open for reading "+ also writing", but it
will not create a new file if one isn't already there; it fails with
an error. POSIX also has this behavior with it's 'open' call, you just
leave the 'create' bit out, and you can open for read/write without
creating.
But I can't figure out how to do this with iostreams. The only
ios:penmode values I know of are: app, ate, binary, in, out,
trunc... and none of them have to do with create-if-doesn't-exist. So
far as I can tell, with iostreams, that is implicit in ios:ut,
whether you want it or not.
Is there a way, or do I have to open the file for reading to check
that it exists, then close it and reopen it for read/write?
Thanks in advance for any advice.