我的应用程序打开文件进行转换并将数据保存到另一个文件..或可能是同一文件..文件大小发生变化,但是我不知道它的大小,直到我看到第一个文件中的数据。
目前,我将文件加载到动态数组中,然后在其中执行所有所需的操作,然后将其保存回去……这一直很好,直到我进入测试阶段,在该阶段我发现要在128mb的系统上转换多个千兆字节的文件ram造成了一些问题...大声笑
这是我的代码。
procedure openfile(fname:string);
var
myfile: file;
filesizevalue:integer;
begin
AssignFile(myfile,fname);
filesizevalue := GetFileSize(fname);
Reset(myFile, 1);
SetLength(dataarray, filesizevalue);
BlockRead(myFile, dataarray[0], filesizevalue);
CloseFile(myfile);
end;
我需要的是直接文件访问以最大程度地减少内存使用。.那就是我认为我需要的/
这是我需要的可以在delphi中完成吗
最佳答案
我将研究使用TFileStream,也许是带有缓冲的TFileStream,但是您实际上需要显示对数据的处理方式,因为很难确定最佳策略。正如gabr所说,一种选择是内存映射文件,其代码位于他的链接中,但由于它是我的代码,因此我也将其添加到这里!
procedure TMyReader.InitialiseMapping(szFilename : string);
var
// nError : DWORD;
bGood : boolean;
begin
bGood := False;
m_hFile := CreateFile(PChar(szFilename), GENERIC_READ, 0, nil, OPEN_EXISTING, 0, 0);
if m_hFile <> INVALID_HANDLE_VALUE then
begin
m_hMap := CreateFileMapping(m_hFile, nil, PAGE_READONLY, 0, 0, nil);
if m_hMap <> 0 then
begin
m_pMemory := MapViewOfFile(m_hMap, FILE_MAP_READ, 0, 0, 0);
if m_pMemory <> nil then
begin
htlArray := Pointer(Integer(m_pMemory) + m_dwDataPosition);
bGood := True;
end
else
begin
// nError := GetLastError;
end;
end;
end;
if not bGood then
raise Exception.Create('Unable to map token file into memory');
end;
关于delphi - 直接在Delphi中访问文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/519916/