Delphi: Alternative to using Reset/ReadLn for text file reading

后端 未结 7 1212
时光取名叫无心
时光取名叫无心 2020-12-14 11:10

i want to process a text file line by line. In the olden days i loaded the file into a StringList:

slFile := TStringList.Create();
slFile.LoadFr         


        
7条回答
  •  攒了一身酷
    2020-12-14 11:46

    Why not simply read the lines of the file directly from the TFileStream itself one at a time ?

    i.e. (in pseudocode):

      readline: 
        while NOT EOF and (readchar <> EOL) do
          appendchar to result
    
    
      while NOT EOF do
      begin
        s := readline
        process s
      end;
    

    One problem you may find with this is that iirc TFileStream is not buffered so performance over a large file is going to be sub-optimal. However, there are a number of solutions to the problem of non-buffered streams, including this one, that you may wish to investigate if this approach solves your initial problem.

提交回复
热议问题