Creating a file that doesn't exist

A

AJ

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::eek:ut);
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
 
J

Jakob Olsen

Instead of rfile, don't you need to use: open??
I'm a newbie, so correct me if i'm wrong...
Jakob
 
T

Thomas Matthews

Jakob said:
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
 
C

Chris Mantoulidis

Jakob Olsen said:
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 :)

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).
 

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

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top