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

05-20-2008, 03: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";



how can i improve the speed of writing to file ?

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

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

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