Read Line with TFileStream delphi

André picture André · Jul 22, 2012 · Viewed 26.3k times · Source

How do I read file using certain lines TFileStream. I read lines which has millions of files. So I want to play in memory that I will only use

Example:

Line 1: 00 00 00 00 00 00 00 00
Line 2: 00 00 00 00 00 00 00 00
Line 3: 00 00 00 00 00 00 00 00
Line 4: 00 00 00 00 00 00 00 00
Line 5: 00 00 00 00 00 00 00 00

I read the line 2 to 4

I used a function TextFile, but it seems slow. Just found a function that reads the last line in TFileStream.

Answer

Sean B. Durkin picture Sean B. Durkin · Jul 22, 2012

You can open a file for reading with the TFileStream class like so ...

FileStream := TFileStream.Create( 'MyBigTextFile.txt', fmOpenRead)

TFileStream is not a reference counted object, so be sure and release it when you are done, like so ...

FileStream.Free

From here-on in, I will assume that your file's character encoding is UTF-8 and that the end-of-line termination is MS style. If not, please adjust accordingly, or update your question.

You can read a single code unit of a UTF-8 character (not the same thing as reading a single character) like so:

var ch: ansichar;
FileStream.ReadBuffer( ch, 1);

You can read a line of text like so ...

function ReadLine( var Stream: TStream; var Line: string): boolean;
var
  RawLine: UTF8String;
  ch: AnsiChar;
begin
result := False;
ch := #0;
while (Stream.Read( ch, 1) = 1) and (ch <> #13) do
  begin
  result := True;
  RawLine := RawLine + ch
  end;
Line := RawLine;
if ch = #13 then
  begin
  result := True;
  if (Stream.Read( ch, 1) = 1) and (ch <> #10) then
    Stream.Seek(-1, soCurrent) // unread it if not LF character.
  end
end;

To read lines 2, 3 and 4, assuming position is at 0 ...

ReadLine( Stream, Line1);
ReadLine( Stream, Line2);
ReadLine( Stream, Line3);
ReadLine( Stream, Line4);