首页  编辑  

如何映射超大型文件

Tags: /超级猛料/Stream.File.流、文件和目录/文件操作/   Date Created:

uses lyhClasses, lyhTools;

procedure TForm1.Button1Click(Sender: TObject);

var

 F : HFILE;

 P : PChar;

 m : THandle;

 s : Int64;

begin

 s := Int64(GetSystemInfoEx.dwAllocationGranularity) * 102400;

 F := CreateFile('C:\Demo.dat', GENERIC_ALL, FILE_SHARE_READ,

   nil, OPEN_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN or FILE_FLAG_RANDOM_ACCESS, 0);

 m := CreateFileMapping(F, nil, PAGE_READWRITE, 2, MaxInt, nil);

 //RaiseLastOSError;

 p := MapViewOfFile(m, FILE_MAP_ALL_ACCESS, Int64Rec(s).Hi, Int64Rec(s).Lo, 102);

 //RaiseLastOSError;

 P^ := 'a';

 UnmapViewOfFile(p);

 CloseHandle(m);

 S := S + 102;

 SetFilePointer(F, Int64Rec(s).Lo, @Int64Rec(s).Hi, soFromBeginning);

 SetEndOfFile(F);

 CloseHandle(F);

end;