Solved

most efficient way to write a log file?

Posted on 1998-12-17
3
454 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now