Solved

How often can I call CFile::Flush(), or how often should I?

Posted on 2014-02-07
5
705 Views
Last Modified: 2014-02-08
I have a program that saves data using a CFile object. I call Write() followed by Flush() every time data is available to be saved. My question is, how often should I be calling Flush()? My program can call the Write() function up to 100 times a second. Is it ok to call Flush() 100 times a second? Will this cause any problems? My program is hanging and I'm looking for likely places where the performance could be problematic.
0
Comment
Question by:goondoo27
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39844127
You can of course call it as often as you want, the performance penalty should be minimal - if there is no data to be written, the function will just return without doing anything. Depending on what kind of data you are writing. You however might want to consider opening the file as 'unbuffered' , so you don't even have to explicitly 'flush' any file buffers.
0
 

Author Comment

by:goondoo27
ID: 39844621
Thanks jkr. There will be data to flush every time I call Flush() because I only call it after a Write(). I'll look at opening the file as unbuffered. So you don't see any performance issue with calling Flush() right after Write() at say 100 to 300 times a second?
0
 
LVL 86

Expert Comment

by:jkr
ID: 39844647
Again, that will depend on the amount of data you are writing. Also, why do you need everyhing to be written immediately? I'd say that flushing all buffers is a good thing to do when a coherent point is reached, e.g. if you were writing audio data, you'd call 'Flush()' when a full song/track is ready (yes, not really a perfect example)...
0
 

Author Comment

by:goondoo27
ID: 39844657
I'm writing a text file. Each Write() will put a single line of statistical data into the file. I'd estimate that up to 200 characters are written each time I call Write(). I originally put the Flush() call in the code so that the stats were immediately saved to the file and could be viewed by programs tailing the text file. Is there a better way to do this?
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39844666
No, that sounds reasonable. And given the amount of data, that shouldn't cause any trouble.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 clear a vector as well as how to detect empty vectors in C++.

863 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

20 Experts available now in Live!

Get 1:1 Help Now