PDA

View Full Version : writing and appending to file lots of times takes to long how to improve ?



umen242
05-20-2008, 02:52 PM
Hello all

im using ofstream to writing to file and then appending strings to file

the problem is that im writing to file many times some thing like 5000+ times ( the reason is it writing to file directories and files )

the code open file and writing to it looks like this :


ofstream myfile ("my_log.txt",ios::app);

if (myfile.is_open())

{

myfile << c <<".\n";

myfile.close();

}



how can i improve the speed of writing to file ?

oracleguy
05-20-2008, 03:35 PM
Don't open and close it so much. Open it once and leave it open until you are done.

umen242
05-20-2008, 06:35 PM
can i close the file only when the application exit ?

oracleguy
05-20-2008, 08:23 PM
Yeah or when you've done your 5,000 writes or whatever. Just make sure you do close it at some point.