Solved

most efficient way to write a log file?

Posted on 1998-12-17
3
459 Views
Last Modified: 2008-02-01
I need to write text to a log file.  I'm always appending to the end of it.  I'm on an NT system.  What is the most efficient way to do this?  CFile?  CStdIoFile?  ofstream?  I'd like to write to the file possibly a few times per second, and would definitely like to flush it to disk from time to time (every 5-60 seconds).
Thanks.
0
Comment
Question by:GoWithTheFlow
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 1180434
As you ask for an efficiant way, i definitely recomment the Win32 file APIs ('CreateFile()', 'WriteFile()' and friends). Whilst they may seem a bit clumsy at first sight, they give you the most control about what's happening - and, every usage of CFile, CStdIoFile, ofstream or anything else will sooner or later end up at this API ... (BTW: 'flushing' is done using 'FlushFileBuffers()' - and if you use 'CreateFile()' in conjunction with 'FILE_FLAG_NO_BUFFERING', the cache is bypassed, so that it is guaranteed that there's no data loss due to caching when there's a crash...)
0
 

Author Comment

by:GoWithTheFlow
ID: 1180435
Is it more efficient to flush periodically, or to use the NO_BUFFERING flag?

I'm also wondering if I could avoid closing and reopening the file all the time. To be sure my data is safely on the disk, do I need to close & reopen the file the file? Or can I leave it open?  In other words, will the file be corrupt if the machine crashes while I have it open?
0
 
LVL 86

Expert Comment

by:jkr
ID: 1180436
Well, it depends on what is more important for you - if application performance is more important, flushing periodically would be the way to go, but if it is data integrity (e.g. you don't want to loose any data), i'd prefer 'FILE_FLAG_NO_BUFFERING'. (BTW: 'FILE_FLAG_NO_BUFFERING' might also come in handy when you need high-performance direct I/O bypassing the file system cache, but this is an issue of I/O performance, e.g. when handling large files, not log files...)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 133
C++ to C# code conversion issue 4 119
Least Squares Curve Fitting 4 114
c++, dynamic object by json 1 60
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question