Simultaneous file access - ReadFile() can't read NTFS filesystem cache, but it could in XP / Win2003
-
If I open a file and write to it, then try to read the file contents in a different process, I'm not able to see the file updates until they get flushed to disk. This behavior is different than it was in Windows XP / 2003. Has anybody else run into this, and is there a way to make this work like it did before? This code illustratest the problem. With the FlushFileBuffers() call, I can see the file contents from a different process. Without it, the file remains unchanged until NTFS decides to flush its cache. // writetest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <Windows.h> int _tmain(int argc, _TCHAR* argv[]) { HANDLE hFile; hFile=CreateFile(L"tst.out", FILE_ALL_ACCESS, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if(hFile == INVALID_HANDLE_VALUE) { printf("Open file failed\n"); exit(1); } while(1) { DWORD dwBytesWritten; WriteFile(hFile, "abcdefg\r\n", strlen("abcdefg\r\n"), &dwBytesWritten, NULL); printf("Wrote to file\n"); //FlushFileBuffers(hFile); Sleep(10000); } }
-
If I open a file and write to it, then try to read the file contents in a different process, I'm not able to see the file updates until they get flushed to disk. This behavior is different than it was in Windows XP / 2003. Has anybody else run into this, and is there a way to make this work like it did before? This code illustratest the problem. With the FlushFileBuffers() call, I can see the file contents from a different process. Without it, the file remains unchanged until NTFS decides to flush its cache. // writetest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <Windows.h> int _tmain(int argc, _TCHAR* argv[]) { HANDLE hFile; hFile=CreateFile(L"tst.out", FILE_ALL_ACCESS, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if(hFile == INVALID_HANDLE_VALUE) { printf("Open file failed\n"); exit(1); } while(1) { DWORD dwBytesWritten; WriteFile(hFile, "abcdefg\r\n", strlen("abcdefg\r\n"), &dwBytesWritten, NULL); printf("Wrote to file\n"); //FlushFileBuffers(hFile); Sleep(10000); } }
If you would have read the documentation for the CreateFile Function[^] you might have found the FILE_FLAG_NO_BUFFERING flag. Additional File Buffering Information[^] Best Wishes, -David Delaune