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 file includes:
- MapTextFile class
- Full source code
Properties and Methods
Creates an instace of the class
Destroys the instance
Opens a file as a memory mapped file.
Close the memory mapped file and frees all handles.
Returns TRUE, if the End of the file is reached.
Returns the Size of the file in Bytes
Returns the actual file read/write position
Sets the actual read/write position
Reads a character from the actual read/write position.
Returns a string, starting at the actual r/w position.
Same as ReadSring, but as a Procedure.
Reads a charater from an arbitray possition.
Reads a line of charactes from the MMF.
Returns a string, starting at an arbitray possition.
Findes a substring in the MMF and Returns the position.
Same as Findstring, but supports wildcard search.
Reads a number of bytes to a anonymous variable.
Writes a number of bytes to the file.