在delphi中快速读取/写入文件

编程入门 行业动态 更新时间:2024-10-22 21:37:54
本文介绍了在delphi中快速读取/写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在以二进制形式将文件加载到数组中,这似乎需要一段时间有没有更好更快更有效的方法来做到这一点.我正在使用类似的方法写回文件.

I am loading a file into a array in binary form this seems to take a while is there a better faster more efficent way to do this. i am using a similar method for writing back to the file.

procedure openfile(fname:string); var myfile: file; filesizevalue,i:integer; begin assignfile(myfile,fname); filesizevalue:=GetFileSize(fname); //my method SetLength(dataarray, filesizevalue); i:=0; Reset(myFile, 1); while not Eof(myFile) do begin BlockRead(myfile,dataarray[i], 1); i:=i+1; end; CloseFile(myfile); end;

推荐答案

您通常不应该逐字节读取文件.使用具有较大值的 BlockRead(通常最好使用 512 或 1024)并使用其返回值来确定读取了多少字节.

You generally shouldn't read files byte for byte. Use BlockRead with a larger value (512 or 1024 often are best) and use its return value to find out how many bytes were read.

如果大小不是太大(并且您对 SetLength 的使用似乎支持这一点),您还可以使用一个 BlockRead 调用一次读取完整的文件.所以,修改你的方法,这将是:

If the size isn't too large (and your use of SetLength seems to support this), you can also use one BlockRead call reading the complete file at once. So, modifying your approach, this would be:

AssignFile(myfile,fname); filesizevalue := GetFileSize(fname); Reset(myFile, 1); SetLength(dataarray, filesizevalue); BlockRead(myFile, dataarray[0], filesizevalue); CloseFile(myfile);

也许您也可以将过程更改为名为 OpenAndReadFile 的布尔函数,如果无法打开或读取文件,则返回 false.

Perhaps you could also change the procedure to a boolean function named OpenAndReadFile and return false if the file couldn't be opened or read.

更多推荐

在delphi中快速读取/写入文件

本文发布于:2023-11-11 06:15:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577601.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快速   文件   delphi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!