Archived

This topic is now archived and is closed to further replies.

File IO question

This topic is 5399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to create a log file with the CFile object in C++. File IO always trips me up and I was wondering if it was better to call CFile::Open() once when the log file is created, and then close it at the end of the program, or if I should open and close the file each time I wrtie to the log. Thnks

Share this post


Link to post
Share on other sites
I would only open the file once myself. I am assuming that you simply wanting to writing to the disk each time you log a message instead of storing the log messages in memory and then flushing them all out to the hard drive.

In general disk IO is slow and should be avoided as much as possible. Usually I end up buffering all the logs in memory, upto a predetermined number, and then when the buffer is full I write them out all at once.

Share this post


Link to post
Share on other sites
quote:
Original post by PaleRaider
I would only open the file once myself. I am assuming that you simply wanting to writing to the disk each time you log a message instead of storing the log messages in memory and then flushing them all out to the hard drive.


Agreed. You may want to sync the stream after each message, if there''s a chance the program could crash and you want the log finalized.

quote:
In general disk IO is slow and should be avoided as much as possible. Usually I end up buffering all the logs in memory, upto a predetermined number, and then when the buffer is full I write them out all at once.


This is overkill. C++ streams already buffer IO for you.


But... but that''s what HITLER would say!!

Share this post


Link to post
Share on other sites