By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

fstream..

P: n/a
hi there
i have been trying to open file using fstream, in read+write+binary mode

fstream ff;

ff.open(filename, ios::in | ios::out | ios::binary);

now this works

what i m looking for is, if the file does not exist, it should be created,
and i dont know what ios flag to use, the above statement will open the file
only if it already exist, for now what i m doing is chaek wether the file
already exist, if it does not exist then it opens file just for writing and
creates a zero byte file and then closes, then i m able to use above
statement for writing and reading from it, m i doing it right, any
suggestion ??

regards
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"dumboo" <vt***@yahoo.com> wrote in message
news:2g************@uni-berlin.de...
hi there
i have been trying to open file using fstream, in read+write+binary mode

fstream ff;

ff.open(filename, ios::in | ios::out | ios::binary);

now this works

what i m looking for is, if the file does not exist, it should be created,
and i dont know what ios flag to use, the above statement will open the file only if it already exist, for now what i m doing is chaek wether the file
already exist, if it does not exist then it opens file just for writing and creates a zero byte file and then closes, then i m able to use above
statement for writing and reading from it, m i doing it right, any
suggestion ??

regards


There's no one set of flags that will do what you want. Try this

ff.open(filename, ios::in | ios::out | ios::binary);
if (!ff.is_open())
{
ff.open(filename, ios::in | ios::out | ios::trunc | ios::binary);
if (!ff.is_open())
error();
}

The second open call will create a zero length file, open for reading and
writing.

john
Jul 22 '05 #2

P: n/a
hi there
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2g************@uni-berlin.de...
[..]
There's no one set of flags that will do what you want. Try this

ff.open(filename, ios::in | ios::out | ios::binary);
if (!ff.is_open())
{
ff.open(filename, ios::in | ios::out | ios::trunc | ios::binary);
if (!ff.is_open())
error();
}

[..]
thats wt i m doing now, it was strange to read that there is no set of flags
for doing such a common operation, any way thankx for your suggestion :-)
regards
Jul 22 '05 #3

P: n/a
dumboo <vt***@yahoo.com> wrote:
thats wt i m doing now, it was strange to read that there is no set of flags
for doing such a common operation, any way thankx for your suggestion :-)
regards


If you compare the available flags with standard C's fopen() family of calls,
you'll notice a striking similarity. :-) fopen() doesn't have this feature
either.

I was surprised to find this as well, but I was so used to POSIX open() that
it caught me off guard.

- Chris

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.