In Delphi you have many ways to manipulate text files. But all of these have a handicap: You must read the whole file (StringList) or you can only access data sequential (Assign/Readln). But there is an alternative methode to access big files: Memory Mapped Files (MFF).

The Windows API provides the so called Memory Mapped Files (MMF). Windows uses MMFs to load EXE or DLL files. Therefore the mechanism is very efficient, but simple to handle. The only handicap is, that you must know the size of the file before you access it. This means for typical text files, that the operation is normally limited to read from the file or manipulate inside.

The class TMapTextfile wraps the neccesary API calls and povides efficent functions for accessing the data (random access, wildcard search and so on).

Download

Download file includes:

  • MapTextFile class
  • Full source code
  • Freeware!

Download

Properties and Methods
Create
Creates an instace of the class
Destroy
Destroys the instance
Open
Opens a file as a memory mapped file.
Close
Close the memory mapped file and frees all handles.
EndOfFile
Returns TRUE, if the End of the file is reached.
GetSize
Returns the Size of the file in Bytes
GetPos
Returns the actual file read/write position
SetPos
Sets the actual read/write position
ReadChar
Reads a character from the actual read/write position.
ReadString
Returns a string, starting at the actual r/w position.
ReadLn
Same as ReadSring, but as a Procedure.
ReadCharAt
Reads a charater from an arbitray possition.
ReadChars
Reads a line of charactes from the MMF.
ReadStringAt
Returns a string, starting at an arbitray possition.
FindString
Findes a substring in the MMF and Returns the position.
FindWildCard
Same as Findstring, but supports wildcard search.
ReadBytes
Reads a number of bytes to a anonymous variable.
WriteBytes
Writes a number of bytes to the file.