f



Creating a file that doesn't exist

Hello, I was wondering if it is possible to create a file if it doean't
exist. I am using fstream and writing in and out a binary file. I check to
see if the file is opened. if it doesn't get opened, i want to create the
file. I was wondering if this can be done. This is the code i have for
opening the file:

fstream rfile("names.db", ios::binary| ios::in | ios::out);
   if ( !rfile ) {
       cerr << "Could not open file 'names.db'\n";
   }

I am hoping to put the creating of the file after the cerr. i hope someone
can help me out with this. thanks.

AJ


0
ajmooney (1)
12/16/2003 8:52:23 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

3 Replies
499 Views

Similar Articles

[PageSpeed] 9

Instead of rfile, don't you need to use: open??
I'm a newbie, so correct me if i'm wrong...
Jakob
"AJ" <ajmooney@shaw.ca> wrote in message
news:bJzDb.722236$pl3.454765@pd7tw3no...
> Hello, I was wondering if it is possible to create a file if it doean't
> exist. I am using fstream and writing in and out a binary file. I check to
> see if the file is opened. if it doesn't get opened, i want to create the
> file. I was wondering if this can be done. This is the code i have for
> opening the file:
>
> fstream rfile("names.db", ios::binary| ios::in | ios::out);
>    if ( !rfile ) {
>        cerr << "Could not open file 'names.db'\n";
>    }
>
> I am hoping to put the creating of the file after the cerr. i hope someone
> can help me out with this. thanks.
>
> AJ
>
>


0
jakobols (2)
12/16/2003 11:53:33 AM
Jakob Olsen wrote:

> "AJ" <ajmooney@shaw.ca> wrote in message
> news:bJzDb.722236$pl3.454765@pd7tw3no...
> 
>>Hello, I was wondering if it is possible to create a file if it doean't
>>exist. I am using fstream and writing in and out a binary file. I check to
>>see if the file is opened. if it doesn't get opened, i want to create the
>>file. I was wondering if this can be done. This is the code i have for
>>opening the file:
>>
>>fstream rfile("names.db", ios::binary| ios::in | ios::out);
>>   if ( !rfile ) {
>>       cerr << "Could not open file 'names.db'\n";
>>   }
>>
>>I am hoping to put the creating of the file after the cerr. i hope someone
>>can help me out with this. thanks.
>>
>>AJ
>>
>>
> Instead of rfile, don't you need to use: open??
> I'm a newbie, so correct me if i'm wrong...
> Jakob
> 
> 
> 
Jakob:
Don't top-post.  Replies are either interspersed or
appended at the bottom, like this one.

The OP is using the one of the fstream constructors. This
is perfectly legal.


OP:
Please note that if an open fails, there may be an operating
system issue that won't permit another file being created.
Try clearing the stream errors (fstream::clear()) and trying
to use the open() method.  I'm guaranteeing that this will
work, since there is no guarantee after the constructor fails.


-- 
Thomas Matthews

C++ newsgroup welcome message:
          http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq:   http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
          http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
     http://www.josuttis.com  -- C++ STL Library book

0
12/16/2003 3:46:45 PM
"Jakob Olsen" <jakobols@tiscali.dk> wrote in message news:<3fdef23d$0$9791$edfadb0f@dread14.news.tele.dk>...
> Instead of rfile, don't you need to use: open??
> I'm a newbie, so correct me if i'm wrong...
> Jakob

He can do both :)

> "AJ" <ajmooney@shaw.ca> wrote in message
> news:bJzDb.722236$pl3.454765@pd7tw3no...
> > Hello, I was wondering if it is possible to create a file if it doean't
> > exist. I am using fstream and writing in and out a binary file. I check to
> > see if the file is opened. if it doesn't get opened, i want to create the
> > file. I was wondering if this can be done. This is the code i have for
> > opening the file:
> >
> > fstream rfile("names.db", ios::binary| ios::in | ios::out);
> >    if ( !rfile ) {
> >        cerr << "Could not open file 'names.db'\n";
> >    }
> >
> > I am hoping to put the creating of the file after the cerr. i hope someone
> > can help me out with this. thanks.
> >
> > AJ
> >
> >

Well at least with ofstream, if you open a file that doesn't exist it
is automatically created... So I guess that is that same for its
parent class (fstream).
0
cmad_x (124)
12/16/2003 4:04:58 PM
Reply: