2

Please suggest me something.

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

Thanks!!!

Michael
  • 475
  • 2
  • 9
  • 17

2 Answers2

2

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.

Dan
  • 1,927
  • 2
  • 24
  • 35
2

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.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • @Rob: What happens if the position you seek to is in the middle of a line? – jpfollenius Jun 08 '11 at 13:55
  • What would be really nice would be an adapter class, descended from TStream, to which you could pass a Stream, a position and a length and the adapter would work as a Stream that read just the specified portion of the file. That would allow you to extend Rob's idea and be able to read any chunk of the file, and not just the end. – David Heffernan Jun 08 '11 at 13:58
  • Then you get the second half of the line, @Smasher. No big deal. Worse is if you seek to the middle of a *character*. But if the code needs to handle either of those situations, then you'll need to parse the file from the beginning (for lines or characters) or the end (for lines). – Rob Kennedy Jun 08 '11 at 14:00
  • If it's a UTF16 file then middle of a char is easy to avoid, just go to an even position. If it's UTF8 then it can still be done without parsing from beginning, but it's a little more involved. No need to parse from beginning (or end) to avoid landing in the middle of a character. – David Heffernan Jun 08 '11 at 14:09
  • Good ideas! I am asking because SydEdit is stuck and application does not respond when I load big files, is there virtual mode for it? – Michael Jun 08 '11 at 14:16
  • I don't know, Michael. That sounds like a topic for a new question. This one asked how to load the last portion of a file by itself. If you really want to know how to manage large files with SynEdit, then you should ask that. (Also, look up the term *XY problem*.) – Rob Kennedy Jun 08 '11 at 14:18
  • @David, if it's Shift-JIS or Big 5, though, it's harder to detect where one character ends and another begins. The only sure way, I believe, is to start from the beginning of the file. – Rob Kennedy Jun 08 '11 at 14:22
  • @Rob Shift-JIS or Big 5? Never heard of either of those. Not sure I want to either...... – David Heffernan Jun 08 '11 at 14:23
  • Ok, it will be new question. Thanks! – Michael Jun 08 '11 at 14:26