how to recreate a file while writing

May 28, 2009 at 2:05pm
Hi,

I'm opening a file using fstream and writing into the file. While write is in process if want to recreate if the file is deleted by some one. Can any one help me in how to do this.

here code comes:

int main()
{
fstream f;
f.open(" log", fstream::app);
for(int i=0; i<20; i++)
{ f<< "HI"<<endl;
sleep(1);
}
f.close();
return 0;
}

so if i delete the log file while running, it has to recreate the file and it has to write into it.
Please help me in how to achive this.

May 28, 2009 at 2:42pm
Re-open the file for every write. This will guarantee that the file is recreated if it is deleted. Otherwise, use a logging tool such as log4cxx and let it handle all this for you. http://logging.apache.org/log4cxx/index.html
May 28, 2009 at 8:28pm
change your flag from, f.open("log",fstream::app) to
f.open("log",ios::in);

are your writing to a txt file, because you have no extension for your file type.
Topic archived. No new replies allowed.