language >> MapViewOfFile and FlushFileBuffers

by benoitlavallee » Sat, 21 Feb 2004 03:16:38 GMT

Hello, I call FlushFileBuffers() and I have views in RAM which I
obtained from MapViewOfFile(). Will my views be synchronized with the
disc after the call?


language >> MapViewOfFile and FlushFileBuffers

by William DePalo [MVP VC++] » Sat, 21 Feb 2004 04:29:12 GMT

I suggest you post again in the kernel group.

But given this remark in the help

A mapped view of a file is not guaranteed to be coherent with a file being
accessed by the ReadFile or WriteFile function:

I think that the answer is no.

FWIW: When I use MMF's I like to view the in-memory copy of the data as
"primary" and the file copy as "backup". With that mindset I tend to use
FlushViewOfFile() to keep the backup up to date.


Similar Threads

1. non admin FlushFileBuffers for a volume

2. Will FlushFileBuffers be automatically called on CloseHandle(hFile)?

To begin with code:

HANDLE hFile = CreateFile(...);
WriteFile(hFile, ...);
ReadFile(hFile, ...);

My queston is: Can CloseHandle(hFile) make sure that all the cached
data of hFile will be flushed to the disk?

I googled it and found nothing. Hope someone can give me some tips.
Thanks in advance.

3. non admin FlushFileBuffers for a volume

4. Slow performance with new 3GHz Machine using FlushFileBuffers

CBFalconer wrote:

> Pedro Salazar wrote:
>>#include "stdafx.h"
>             ^^^^^^^^ Unknown file
>>#include <stdio.h>
>>#include <io.h>
>             ^^^^ Non standard file
>>#include <windows.h>
>             ^^^^^^^^^ Non-standard file
>>int main(int argc, char* argv[])
> This is neither a C nor a C++ program,
> as defined for those newsgroups.
> They deal ONLY with ISO standard, fully portable,
> fully portable, programs.

Nonsense!  Where is it written that
"Only standard header files may be included in C or C++ programs?"

         > cat windows.h
         > cat stdafx.h
         > cat io.h
         typedef FILE*   HANDLE;

         HANDLE _fileno(FILE* fp) {
           return (HANDLE)fp;

         HANDLE  _get_osfhandle(HANDLE h) {
           return h;

         int FlushFileBuffers(HANDLE h) {
           return fflush((FILE*)h);

         > gcc -Wall -std=c99 -pedantic -I. -o pedro pedro.c
         > time ./pedro
         0.019u 0.060s 0:00.08 87.5%     0+0k 0+0io 79pf+0w
         > gcc --version
         gcc (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)

5. FlushFileBuffers

6. FlushFileBuffers on iostream

Is it possible to obtain a Windows HANDLE for a file
that I've opened as a C++ fstream, so that I can call
FlushFileBuffers on it?

I'm not sure whether fstream::sync (or stdio fflush)
calls this function; I think those functions just
flush data from the C or C++ library buffering, to
the operating system. The FlushFileBuffers causes
the operating system to ensure the data has been
sent to the disk.

7. FileStream & MapViewOfFile comparison

8. MapViewOfFile...

Hi !
I have a problem with the MapViewOfFile API.
I use this API to store data in files in my own database system management ;
when I need to store new data, is the file is NOT large enought, I close the
file, reopen and grow it (CreateFileMapping) and re-call MapViewOfFile (last
argument is 0, so I map entire file) ; sometimes, it returns NULL and
GetLastError returns 8 (not enought space). I test it with differents
computers, with 512 Mb RAM to 2Gb RAM, with large swap files, but the
problem seems not to be linked to memory... More over, the memory occupied
by my application is always less that physical memory...
It seems to happen when file is about 250 Gb, but it's not systematic...
More over, it seems to have differents behaviours between Win2000 and WinXP.
Is this a problem due to memory fragmentation ?
Thanks for any help...

Emmanuel Derriey