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:
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;
}
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:
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;
}