Question
Does anyone know the fastest way to read large text files (10Mb) into a string.Readln is just too slow.
 

Answer 1

You may try this:

function R(const FileName: string): string;
var
M: TFileStream;
begin
M := TFileStream.Create(FileName, fmOpenRead);
try
SetLength(Result, M.Size);
M.Read(Result[1], M.Size);
finally
M.Free;
end;
end;

Answer 2

As an alternative to Christian's suggestion, you can also use a memory-mapped file:

function MMFileToString(const AFilename: string): string;
var
hFile: THandle;
hFileMap: THandle;
hiSize: DWORD;
loSize: DWORD;
text: string;
view: pointer;
begin
Result := '';
if AFilename = '' then
Exit;
if not FileExists(AFilename) then
Exit;
{Open the file}
hFile := CreateFile(
PChar(AFilename), GENERIC_READ, FILE_SHARE_READ, nil,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
);
if hFile <> INVALID_HANDLE_VALUE then
begin
loSize := GetFileSize(hFile, @hiSize);
{File was opened successfully, now map it:}
hFileMap := CreateFileMapping(
hFile, nil, PAGE_READONLY, hiSize, loSize, 'TextForString'
);
if (hFileMap <> 0) then
begin
if (GetLastError() = ERROR_ALREADY_EXISTS) then
begin
MessageDlg(
'Mapping already exists - not created.', mtWarning, [mbOk], 0
);
CloseHandle(hFileMap)
end
else
begin
try
{File mapped successfully, now map a view of the file into the
address space:}
view := MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);
if (view <> nil) then
begin {View mapped successfully}
{Close file handle - as long is view is open it will persist}
CloseHandle(hFile);
SetLength(Result, loSize);
Move(view^, Result[1], loSize);
end
else
MessageDlg(
'Unable to map view of file. ' + SysErrorMessage(GetLastError),
mtWarning, [mbOk], 0
);
finally
UnmapViewOfFile(view); {Close view}
CloseHandle(hFileMap); {Close mapping}
end
end
end
else
begin
MessageDlg(
'Unable to create file mapping. ' + SysErrorMessage(GetLastError),
mtWarning, [mbOk], 0
);
end;
end
else
begin
MessageDlg(
'Unable to open file. ' + SysErrorMessage(GetLastError),
mtWarning, [mbOk], 0
);
end;
end;

How to read very large text files fast的更多相关文章

  1. Writing Text Files On The Client in Oracle Forms 10g

    Below is the example to write file on client in Oracle Forms 10g with webutil library package.Note:  ...

  2. Access text files using SQL statements by DB Query Analyzer

    Access text files using SQL statements by DB Query Analyzer Ma Gen feng (Guangdong Unitoll Services ...

  3. LaF: Fast Access to Large ASCII Files

    貌似可以随机读取dataframe格式的文本文件.

  4. tomcat gzip compression not working for large js files

    solution 1: <Connector port="8080" protocol="HTTP/1.1" connectionTimeout=&quo ...

  5. How to remove duplicate lines in a large text file?

    How would you remove duplicate lines from a file that is  much too large to fit in memory? The dupli ...

  6. text files and binary files

    https://en.wikipedia.org/wiki/Text_file https://zh.wikipedia.org/wiki/文本文件

  7. interleave two text files with specified lines

    a_file=$1 a_step=$2 b_file=$3 b_step=$4 a_start=1 let a_end=$a_start+$a_step b_start=1 let b_end=$b_ ...

  8. Convert between Unix and Windows text files - IU Knowledge Base from: https://kb.iu.edu/d/acux

    vi. To input the ^M character, press Ctrl-v , and then press Enter or return . In vim, use :set ff=u ...

  9. GIT之二 基础篇(1)

    GIT基础 取得项目的 Git 仓库 有两种取得 Git 项目仓库的方法.第一种是在现存的目录下,通过导入所有文件来创建新的 Git 仓库.第二种是从已有的 Git 仓库克隆出一个新的镜像仓库来. 在 ...

随机推荐

  1. 利用Python玩微信跳一跳

    创建python项目jump_weixin,新建python程序jump.py 需要4个辅助文件[adb.exe,AdbWinApi.dll,AdbWinUsbApi.dll,fastboot.exe ...

  2. C#连接Oracle数据库的连接字符串

    来源:http://blog.csdn.net/superhoy/article/details/8108037 两种方式:1.IP+SID方式 2.配置链接方式 1..IP+SID方式 DbHelp ...

  3. Hadoop通过路径和和链接访问HDFS

    如果既想在Hadoop服务器本地可以通过绝对路径如"/user/hadoop"方式访问hdfs,也想通过"hdfs://local host:9000/user/hado ...

  4. 中点Brehensam画线算法

    #include<stdio.h> #include<stdlib.h> #include"graphics.h" //函数声明 void MidBreha ...

  5. 2018-2019 网络对抗技术 20165226 Exp4:恶意代码分析

    2018-2019 网络对抗技术 20165226 Exp4:恶意代码分析 目录 一.实验内容说明及基础问题回答 二.实验过程 Task1 系统运行监控 结合windows计划任务与netstat命令 ...

  6. [转载] ./configure,make,make install的作用

    1.configure,这一步一般用来生成 Makefile,为下一步的编译做准备,你可以通过在 configure 后加上参数来对安装进行控制,比如代码:./configure –prefix=/u ...

  7. ubuntu 常用命令集锦

    一.文件/文件夹管理 ls 列出当前目录文件(不包括隐含文件) ls -a 列出当前目录文件(包括隐含文件) ls -l 列出当前目录下文件的详细信息 cd .. 回当前目录的上一级目录 cd - 回 ...

  8. [UE4]Acotr

    任何能被放在关卡中的对象都是Actor Tick是每帧都会调用的事件

  9. 基于标准库实现string和wstring的转换

    // convert string to wstring std::wstring to_wstring(const std::string& str, const std::locale&a ...

  10. 【Python编程:从入门到实践】chapter4 操作列表

    chapter4 操作列表 4.1 遍历整个列表 magicians=['alice','david','carolina'] for magician in magicians: print(mag ...