Note that there are some explanatory texts on larger screens.

plurals
  1. POHow Can I Efficiently Read The FIrst Few Lines of Many Files in Delphi
    primarykey
    data
    text
    <p>I have a "Find Files" function in my program that will find text files with the .ged suffix that my program reads. I display the found results in an explorer-like window that looks like this:</p> <p><img src="https://i.stack.imgur.com/t9DbY.jpg" alt="enter image description here"></p> <p>I use the standard FindFirst / FindNext methods, and this works very quickly. The 584 files shown above are found and displayed within a couple of seconds.</p> <p>What I'd now like to do is add two columns to the display that shows the "Source" and "Version" that are contained in each of these files. This information is found usually within the first 10 lines of each file, on lines that look like:</p> <pre><code>1 SOUR FTM 2 VERS Family Tree Maker (20.0.0.368) </code></pre> <p>Now I have no problem parsing this very quickly myself, and that is not what I'm asking.</p> <p>What I need help with is simply how to most quickly load the first 10 or so lines from these files so that I can parse them.</p> <p>I have tried to do a StringList.LoadFromFile, but it takes too much time loading the large files, such at those above 1 MB. </p> <p>Since I only need the first 10 lines or so, how would I best get them?</p> <p>I'm using Delphi 2009, and my input files might or might not be Unicode, so this needs to work for any encoding.</p> <hr> <p>Followup: Thanks Antonio,</p> <p>I ended up doing this which works fine:</p> <pre><code>var CurFileStream: TStream; Buffer: TBytes; Value: string; Encoding: TEncoding; try CurFileStream := TFileStream.Create(folder + FileName, fmOpenRead); SetLength(Buffer, 256); CurFileStream.Read(Buffer[0], 256); TEncoding.GetBufferEncoding(Buffer, Encoding); Value := Encoding.GetString(Buffer); ... (parse through Value to get what I want) ... finally CurFileStream.Free; end; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload