Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows API
  4. Simultaneous file access - ReadFile() can't read NTFS filesystem cache, but it could in XP / Win2003

Simultaneous file access - ReadFile() can't read NTFS filesystem cache, but it could in XP / Win2003

Scheduled Pinned Locked Moved Windows API
c++helpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    m1tzep
    wrote on last edited by
    #1

    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); } }

    L 1 Reply Last reply
    0
    • M m1tzep

      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); } }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups