Solved

most efficient way to write a log file?

Posted on 1998-12-17
3
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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 the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 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