首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

内存映射文件有关问题

2012-03-28 
内存映射文件问题Delphi(Pascal) code{Copyright ?1999 by Delphi 5 Developers Guide - Xavier Pacheco

内存映射文件问题

Delphi(Pascal) code
{Copyright ?1999 by Delphi 5 Developer's Guide - Xavier Pacheco and Steve Teixeira}unit MainFrm;interfaceuses  Windows, Messages, SysUtils, Classes, Graphics, Controls,  Forms, Dialogs, StdCtrls;const  FName = 'test.txt';type  TMainForm = class(TForm)    btnUpperCase: TButton;    memTextContents: TMemo;    lblContents: TLabel;    btnLowerCase: TButton;    procedure btnUpperCaseClick(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure btnLowerCaseClick(Sender: TObject);  public    UCase: Boolean;    procedure ChangeFileCase;  end;var  MainForm: TMainForm;implementation{$R *.DFM}procedure TMainForm.btnUpperCaseClick(Sender: TObject);begin  UCase := True;  ChangeFileCase;end;procedure TMainForm.btnLowerCaseClick(Sender: TObject);begin  UCase := False;  ChangeFileCase;end;procedure TMainForm.FormCreate(Sender: TObject);begin  memTextContents.Lines.LoadFromFile(FName);  // Change to upper case by default.  UCase := True;end;procedure TMainForm.ChangeFileCase;var  FFileHandle: THandle; // Handle to the open file.  FMapHandle: THandle;  // Handle to a file-mapping object  FFileSize: Integer;   // Variable to hold the file size.  FData: PByte;         // Pointer to the file's data when mapped.  PData: PChar;         // Pointer used to reference the file data.begin  { First obtain a file handle to the file to be mapped. This code    assumes the existence of the file. Otherwise, you can use the    FileCreate() function to create a new file. }  if not FileExists(FName) then    raise Exception.Create('File does not exist.')  else    FFileHandle := FileOpen(FName, fmOpenReadWrite);  // If CreateFile() was not successful, raise an exception  if FFileHandle = INVALID_HANDLE_VALUE then    raise Exception.Create('Failed to open or create file');  try    { Now obtain the file size which we will pass to the other file-      mapping functions. We'll make this size one byte larger as we      need to append a null-terminating character to the end of the      mapped-file's data.}    FFileSize := GetFileSize(FFileHandle, Nil);   { Obtain a file-mapping object handle. If this function is not      successful, then raise an exception. }    FMapHandle := CreateFileMapping(FFileHandle, nil,       PAGE_READWRITE, 0, FFileSize, nil);    if FMapHandle = 0 then      raise Exception.Create('Failed to create file mapping');  finally    // Release the file handle    CloseHandle(FFileHandle);  end;  try    { Map the file-mapping object to a view. This will return a pointer      to the file data. If this function is not successful, then raise      an exception. }    FData := MapViewOfFile(FMapHandle, FILE_MAP_ALL_ACCESS, 0, 0, FFileSize);    if FData = Nil then      raise Exception.Create('Failed to map view of file');  finally    // Release the file-mapping object handle    CloseHandle(FMapHandle);  end;  try    PData := PChar(FData);//************************************    inc(PData, FFileSize);//************************************    // Append a null-terminating character to the end of the file's data    PData^ := #0;    // Now set all characters in the file to uppercase    if UCase then      StrUpper(PChar(FData))    else      StrLower(PChar(FData));  finally    // Release the file mapping.    UnmapViewOfFile(FData);  end;  memTextContents.Lines.Clear;  memTextContents.Lines.LoadFromFile(FName);end;end.

  PData := PChar(FData);
  inc(PData, FFileSize);
这两句话怎么理解?

[解决办法]
PData := PChar(FData);强制指针转换,也就是将PByte强制转换为PChar指针。
inc(PData,FFilesize); 将PData加上一个整数FFilesize,可以等效于下列语句:
PData := PData + FFilesize;
------解决方案--------------------


噢,我的天呀
分析中.........

热点排行