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

Creating a file that doesn't exist

P: n/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::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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Instead of rfile, don't you need to use: open??
I'm a newbie, so correct me if i'm wrong...
Jakob
"AJ" <aj******@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

Jul 22 '05 #2

P: n/a
Jakob Olsen wrote:
"AJ" <aj******@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

Jul 22 '05 #3

P: n/a
"Jakob Olsen" <ja******@tiscali.dk> wrote in message news:<3f**********************@dread14.news.tele.d k>...
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" <aj******@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).
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.