Author: Tomas Rutkauskas
Does anyone know the fastest way to read large text files (10Mb) into a string.
Readln is just too slow.
Answer:
Solve 1:
You may try this:
1 function R(const FileName: string): string;
2 var
3 M: TFileStream;
4 begin
5 M := TFileStream.Create(FileName, fmOpenRead);
6 try
7 SetLength(Result, M.Size);
8 M.read(Result[1], M.Size);
9 finally
10 M.Free;
11 end;
12 end;
Solve 2:
As an alternative to Christian's suggestion, you can also use a memory-mapped file:
13
14 function MMFileToString(const AFilename: string): string;
15 var
16 hFile: THandle;
17 hFileMap: THandle;
18 hiSize: DWORD;
19 loSize: DWORD;
20 text: string;
21 view: pointer;
22 begin
23 Result := '';
24 if AFilename = '' then
25 Exit;
26 if not FileExists(AFilename) then
27 Exit;
28 {Open the file}
29 hFile := CreateFile(PChar(AFilename), GENERIC_READ, FILE_SHARE_READ, nil,
30 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
31 if hFile <> INVALID_HANDLE_VALUE then
32 begin
33 loSize := GetFileSize(hFile, @hiSize);
34 {File was opened successfully, now map it:}
35 hFileMap := CreateFileMapping(hFile, nil, PAGE_READONLY, hiSize,
36 loSize, 'TextForString');
37 if (hFileMap <> 0) then
38 begin
39 if (GetLastError() = ERROR_ALREADY_EXISTS) then
40 begin
41 MessageDlg('Mapping already exists - not created.', mtWarning, [mbOk], 0);
42 CloseHandle(hFileMap)
43 end
44 else
45 begin
46 try
47 {File mapped successfully, now map a view of the file into
48 the address space:}
49 view := MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);
50 if (view <> nil) then
51 begin {View mapped successfully}
52 CloseHandle(hFile);
53 {Close file handle - as long is view is open it will persist}
54 SetLength(Result, loSize);
55 Move(view^, Result[1], loSize);
56 end
57 else
58 MessageDlg('Unable to map view of file. ' +
59 SysErrorMessage(GetLastError),
60 mtWarning, [mbOk], 0);
61 finally
62 UnmapViewOfFile(view); {Close view}
63 CloseHandle(hFileMap); {Close mapping}
64 end
65 end
66 end
67 else
68 begin
69 MessageDlg('Unable to create file mapping. ' + SysErrorMessage(GetLastError),
70 mtWarning, [mbOk], 0);
71 end;
72 end
73 else
74 begin
75 MessageDlg('Unable to open file. ' + SysErrorMessage(GetLastError),
76 mtWarning, [mbOk], 0);
77 end;
78 end;
|