将文本文件读入数组 [英] Read a text file into an array
本文介绍了将文本文件读入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将文本文件从磁盘上的文件读取到内存中的数组中?
How would I go about reading a text file from a file on disk into an array in memory?
此外,我注意到使用ReadLn仅显示第一行(似乎很明显,因为它是ReadLn,但是我应该如何阅读整个文本文档?)
Also, I noticed using ReadLn only shows the first line (seems kind of obvious, since it is ReadLn, but how would I go about reading the whole text document?)
推荐答案
function ReadFile(const FileName: string): string;
var
Strings: TStringList;
begin
Strings := TStringList.Create;
try
Strings.LoadFromFile(FileName);
Result := Strings.Text;
finally
Strings.Free;
end;
end;
该文件以字符串形式返回文件内容,该字符串可以被索引,因此可以认为是数组
That returns the file's contents in a string which can be indexed and so could be considered to be an array.
或者您可能想要一个字符串数组而不是一个字符数组,在这种情况下,直接使用字符串列表是最简单的:
Or perhaps you want an array of strings rather than an array of characters, in which case it's easiest just to use the string list directly:
var
Strings: TStringList;
...
Strings := TStringList.Create;
try
Strings.LoadFromFile(FileName);
//can now access Strings[0], Strings[1], ..., Strings[Strings.Count-1]
finally
Strings.Free;
end;
这篇关于将文本文件读入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文