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

nocreate problem

P: n/a
Hello all,
I have the following problem.
I have a function which being called several times during the run of
my program. When the function is called for the first time I want it
to create a file and write something to that file. Regardless if the
file
exist or not. If it exist it should truncate it and write it as if it
has
not existed. But when the next time I call the function I want it to
test
if the file still exist or not. If not it should not create it. If it
does exist then I want it to append the file. But std does not support
nocreate anylonger. But how can I solve this problem now ??
Thank you in advance.
Robert

void writefile()
{
std::ofstream fout;
if( forthefirstime )
{
fout.open( FileName.c_str(), std::ios::trunc );
if( !fout )
{
std::cerr<<"can't open file";
}
fout<<"this is my first sentence"<<std::endl;
}
else
{
fout.open( FileName.c_str(), std::ios::nocreate | std::ios::app );

if( !fout )
{
std::cerr<<"can't open file";
}
fout<<"this is the next sentence"<<std::endl;

}
fout.close();
forthefirsttime = false;
}

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
wo*********@yahoo.com wrote:
Hello all,
I have the following problem.
I have a function which being called several times during the run of
my program. When the function is called for the first time I want it
to create a file and write something to that file. Regardless if the
file
exist or not. If it exist it should truncate it and write it as if it
has
not existed. But when the next time I call the function I want it to
test
if the file still exist or not. If not it should not create it. If it
does exist then I want it to append the file. But std does not support
nocreate anylonger. But how can I solve this problem now ??
Thank you in advance.
Robert

void writefile()
{
std::ofstream fout;
if( forthefirstime )
{
fout.open( FileName.c_str(), std::ios::trunc );
if( !fout )
{
std::cerr<<"can't open file";
}
fout<<"this is my first sentence"<<std::endl;
}
else
{
fout.open( FileName.c_str(), std::ios::nocreate | std::ios::app );

if( !fout )
{
std::cerr<<"can't open file";
}
fout<<"this is the next sentence"<<std::endl;

}
fout.close();
forthefirsttime = false;
}


instead of opening with ios::nocreate open it with ios::in. If this
fails the file does not exist. if it exists you can reopen it for
writing...

Tom
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.