Michael
Michael

Reputation: 485

Delphi. SynEdit - load last 500 KB of file

Please suggest me something.

How can I load into UniSynEdit/SynEdit last 500 KB of file if it is more then 500 KB?

Thanks!!!

Upvotes: 2

Views: 427

Answers (2)

Rob Kennedy
Rob Kennedy

Reputation: 163357

Create a TFileStream and seek to the position you want to load from, and then pass the stream to the edit control. It should load from the current position.

var
  stream: TStream;
begin
  stream := TFileStream.Create(filename, fmOpenRead);
  try
    stream.Seek(-500 * 1024, soEnd);
    edit.Lines.LoadFromStream(stream);
  finally
    stream.Free;
  end;
end;

Beware that if the file is encoded as UTF-8 or something else that uses a variable number of bytes per character, it isn't safe to jump to arbitrary positions in the file. You might jump to a byte that represents the second half of a two-byte sequence, and then all the subsequent characters you read could be interpreted incorrectly. ANSI and UTF-16 files don't have that danger.

Upvotes: 2

Dan
Dan

Reputation: 1959

One option you have is to copy the last 500 KB of the file into a temporary file and then ask synEdit to process the temporary file.

Upvotes: 2

Related Questions