Solved

most efficient way to write a log file?

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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