Results 1 to 5 of 5

Thread: Is FlushFileBuffer absolutely needed?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Is FlushFileBuffer absolutely needed?

    If I open a file with CreateFile, write to it with WriteFile, and then close it with CloseHandle, doesn't this automatically cause the data to get written to the file? Or is there some possibility that some data will not get written to the file before the file is closed, and thus there will be missing data near the end of the file? I'd like to know just how mandatory it is to include a FlushFileBuffer function when using the API to write to a file. I know for certain that there is no flush command needed when using the internal VB6 commands for filewriting, but am not sure about if it is necessary to do a flush when using the API commands for file writing.

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Is FlushFileBuffer absolutely needed?

    If this is related to your other recent thread then yes it is required. Had you used FILE-FLAG-NO-BUFFERING and FILE-FLAG-WRITE-THROUGH in the CreateFile then as there is no buffering the flush would not be required. More info can be obtained from the FlushFileBuffer documentation.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: Is FlushFileBuffer absolutely needed?

    Interestingly, I've noticed that I get output to a file even if I don't flush the buffer. Is it just "improper" to do this, or can it lead to actual problems? If it can lead to problems, what kind of problems can occur if you close the file immediately after writing to the file, without flushing the buffer prior to closing the file?

    Also does it need to be used after every write operation, or do I only need to use the flush after the last write operation, before I close the file with CloseHandle?

    And what if I'm using overlap method (I'm not, but what if I was)? Would that change how I would use the FlushFileBuffer API call?
    Last edited by Ben321; May 2nd, 2014 at 07:05 PM.

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Is FlushFileBuffer absolutely needed?

    I've got a feeling that the CloseHandle will flush the buffer. The main reason for using FlushFileBuffer is to ensure that data is committed to the device. So if there was some critical data that *must* be written to the hardware you'd issue the Write followed by the flush. Using flush in this way will impact performance, hence the option of having no buffering.

  5. #5
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: Is FlushFileBuffer absolutely needed?

    CloseHandle() will cause all unwritten buffered data to be written.
    Using FlushFileBuffer() will cause all unwritten buffered data to be written. It only needs to be used in cases were you need to be absolutely sure that the data has been written as opposed to just being placed in the buffer. In 'normal' programming, there is no need to use FlushFileBuffer() as even if CloseHandle() is not used, all unwritten buffered data will be written when the program closes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width