nocreate problem

W

wongjoekmeu

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::eek:fstream 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;
}
 
T

Thomas Maier-Komor

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::eek:fstream 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
 

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
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top