Delphi large file read and write use MapViewOfFile

Source: Internet
Author: User

UnitFilemap;InterfaceusesWindows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;ConstFileName='MyTest.txt';typeTForm1=class(tform) Btnupper:tbutton;    Btnlower:tbutton;    Txtcontent:tmemo;    Label1:tlabel;    Btnclose:tbutton; procedureformcreate (Sender:tobject); procedureBtnupperclick (Sender:tobject); procedureBtnlowerclick (Sender:tobject); procedureBtncloseclick (Sender:tobject); Private    {Private Declarations}   PublicUcase:boolean; procedurechangefilecase; End;varForm1:tform1;Implementation{$R *.DFM}//TxtContent.Lines.LoadFromFile (FileName) in the full text is intended only to show file modification resultsproceduretform1.formcreate (sender:tobject);begintxtcontent.clear ();    TxtContent.Lines.LoadFromFile (FileName); //turn it to uppercase by defaultUCase: =True;End;procedureTform1.btnupperclick (sender:tobject);beginUCase:=true; Changefilecase;End;procedureTform1.btnlowerclick (sender:tobject);beginUCase:=false; Changefilecase;End;procedureTform1.changefilecase;//This is the core operations section of this sample filevarFfilehandle:thandle;    Fmaphandle:thandle;    Ffilesize:integer;    Fdata:pbyte; Pdata:pchar;begin//-------------------1 Determine if the file exists----------------------//    if  notFileExists (FileName) Then        RaiseException.Create('file does not exist!')    Else//-------------------2 Get a file using FileOpen handle--------//Ffilehandle: =FileOPen (Filename,fmopenreadwrite); //throws an exception if a file fails to be created    ifFfilehandle = Invalid_handle_value Then        RaiseException.Create('failed to open or create file! ');//-------------------3 using createfilemapping to create a file-mapping object for the handle that you just created with FileOpen--//    Tryffilesize:= GetFileSize (Ffilehandle,Nil); Fmaphandle:= CreateFileMapping (Ffilehandle,Nil, Page_readwrite,0, Ffilesize,Nil); ifFmaphandle =0  Then            RaiseException.Create('failed to create memory-mapped file! '); finallyCloseHandle (Ffilehandle); End;//-------------------4 Use MapViewOfFile to create a view of the createfilemapping established handle mapping file toThe address space of the process------//    TryFData:= MapViewOfFile (fmaphandle,file_map_all_access,0,0, ffilesize); ifFData =Nil  Then            RaiseException.Create('mapping file failed!'); finallyCloseHandle (Fmaphandle); End;//-------------------5 to perform the corresponding operation on the pointer returned by the mapviewoffile------//    TryPData:=PChar (FData);        Inc (Pdata,ffilesize); pdata^:= #0; ifUCase ThenStrupper (PChar (FData))ElseStrlower (PChar (FData)); finally//-------------------6 Canceling the mapping of the File View------//UnmapViewOfFile (FData); End;    TxtContent.Lines.Clear (); TxtContent.Lines.LoadFromFile (fileName);End;procedureTform1.btncloseclick (sender:tobject);beginform1.close ();End;End.{-------------------------------------------------------//When you do a pointer-specific operation, you can use this to directly get the value of a type at that pointer procedure     Tform1.button1click (Sender:tobject); var I:integer;   P:pointer;begin I: = 12345678;   P: = @i;   ShowMessage (IntToStr (p));   My Computer on 1242664, this is an address showmessage (IntToStr (Pinteger (p) ^)); 12345678 ShowMessage (IntToStr (Pword (p) ^)); 24910end; Pinteger (p) ^ indicates that pointer p obtains an integer value Pword (p) ^ indicates that a word value is obtained at pointer P

Many pointer types are also defined in Windows.pas and System.pas as follows Pinteger = ^integer; {$NODEFINE Pinteger} {defined in sysmac.h}pcardinal=^cardinal; Pword=^word; Psmallint= ^smallint;{$NODEFINE Psmallint} {defined in sysmac.h} {$POINTERMATH on}pbyte= ^byte;{$NODEFINE pbyte} {defined in sysmac.h} {$POINTERMATH OFF}Pshortint= ^shortint;{$NODEFINE Pshortint} {defined in sysmac.h}PInt64= ^int64;{$NODEFINE PInt64} {defined in sysmac.h}PUInt64=^uint64; Plongword= ^longword;{$NODEFINE Plongword} {defined in sysmac.h}Psingle= ^single;{$NODEFINE Psingle} {defined in sysmac.h}pdouble= ^double;{$NODEFINE pdouble} {defined in sysmac.h}PDate= ^doub

Delphi large file read and write use MapViewOfFile

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.