title: Delphi: Alternative to using Reset/ReadLn for text file reading
body: <p>i want to process a text file line by line. In the olden days i loaded the file into a <code>StringList</code>:</p>
  
  <pre><code>slFile := TStringList.Create();
  slFile.LoadFromFile(filename);
  
  for i := 0 to slFile.Count-1 do
  begin
     oneLine := slFile.Strings[i];
     //process the line
  end;
  </code></pre>
  
  <p>Problem with that is once the file gets to be a few hundred megabytes, i have to allocate a <em>huge</em> chunk of memory; when really i only need enough memory to hold one line at a time. (Plus, you can't really indicate progress when you the system is locked up loading the file in step 1).</p>
  
  <p>The i tried using the native, and recommended, file I/O routines provided by Delphi:</p>
  
  <pre><code>var
     f: TextFile;
  begin
     Reset(f, filename);
     while ReadLn(f, oneLine) do
     begin
         //process the line
     end;
  </code></pre>
  
  <p>Problem with<code>Assign</code> is that there is no option to read the file without locking (i.e. <code>fmShareDenyNone</code>). The former <code>stringlist</code> example doesn't support no-lock either, unless you change it to <code>LoadFromStream</code>:</p>
  
  <pre><code>slFile := TStringList.Create;
  stream := TFileStream.Create(filename, fmOpenRead or fmShareDenyNone);
     slFile.LoadFromStream(stream);
  stream.Free;
  
  for i := 0 to slFile.Count-1 do
  begin
     oneLine := slFile.Strings[i];
     //process the line
  end;
  </code></pre>
  
  <p>So now even though i've gained no locks being held, i'm back to loading the entire file into memory.</p>
  
  <p>Is there some alternative to <code>Assign</code>/<code>ReadLn</code>, where i can read a file line-by-line, without taking a sharing lock?</p>
  
  <p>i'd rather not get directly into Win32 <code>CreateFile</code>/<code>ReadFile</code>, and having to deal with allocating buffers and detecting <code>CR</code>, <code>LF</code>, <code>CRLF</code>'s.</p>
  
  <p>i thought about memory mapped files, but there's the difficulty if the entire file doesn't fit (map) into virtual memory, and having to maps views (pieces) of the file at a time. Starts to get ugly.</p>
  
  <p>i just want <code>Reset</code> with <code>fmShareDenyNone</code>!</p>