fstream..

D

dumboo

hi there
i have been trying to open file using fstream, in read+write+binary mode

fstream ff;

ff.open(filename, ios::in | ios::eek:ut | 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
 
J

John Harrison

dumboo said:
hi there
i have been trying to open file using fstream, in read+write+binary mode

fstream ff;

ff.open(filename, ios::in | ios::eek:ut | 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::eek:ut | ios::binary);
if (!ff.is_open())
{
ff.open(filename, ios::in | ios::eek:ut | 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
 
D

dumboo

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

ff.open(filename, ios::in | ios::eek:ut | ios::binary);
if (!ff.is_open())
{
ff.open(filename, ios::in | ios::eek:ut | 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
 
C

Chris

dumboo said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problem mixing read and write on io fstream 1
Multiple files in one 8
fstream Buffers 26
fstream File i/o 1
problem with fstream 3
Help with binary I/O 4
fstream - write a file 3
Problem with fstream 4

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top