我有一个大文本文件(大约100MB),每行由CR字符分隔,而不是CRLF.
我尝试使用TStringList.LoadFromFile()或ReadLn(F,..)逐行读取此文本文件,但这两种方法都要求行由CRLF分隔.
谢谢.
PS:我使用的是Delphi 7.
解决方法
这应该做到这一点.
将文本文件读入内存流.
然后用内容填充字符串列表.
textList.Text接受CR,LF和CRLF的任意组合以形成一条线.
将文本文件读入内存流.
然后用内容填充字符串列表.
textList.Text接受CR,LF和CRLF的任意组合以形成一条线.
- function MemoryStreamToString( M : TMemoryStream) : string;
- begin
- SetString( Result,PChar(M.Memory),M.Size div SizeOf(Char)); // Works in all Delphi versions
- end;
- var
- memStream : TMemoryStream;
- textList : TStringList;
- begin
- textList := TStringList.Create;
- try
- memStream:= TMemoryStream.Create;
- try
- memStream.LoadFromFile('mytextfile.txt');
- textList.Text := MemoryStreamToString( memStream); // any combination of CR,LF,CRLF interprets as a line
- finally
- memStream.Free;
- end;
- // do something with textList
- finally
- textList.Free;
- end;
- end;