title: How can I get this File Writing code to work with Unicode (Delphi)
body: <p>I had some code before I moved to Unicode and Delphi 2009 that appended some text to a log file a line at a time:</p>
  
  <pre><code>procedure AppendToLogFile(S: string);
  // this function adds our log line to our shared log file
  // Doing it this way allows Wordpad to open it at the same time.
  var F, C1 : dword;
  begin
    if LogFileName &lt;&gt; '' then begin
      F := CreateFileA(Pchar(LogFileName), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_ALWAYS, 0, 0);
      if F &lt;&gt; 0 then begin
        SetFilePointer(F, 0, nil, FILE_END);
        S := S + #13#10;
        WriteFile(F, Pchar(S)^, Length(S), C1, nil);
        CloseHandle(F);
      end;
    end;
  end;
  </code></pre>
  
  <p>But CreateFileA and WriteFile are <a href="https://forums.embarcadero.com/thread.jspa?threadID=29803&amp;tstart=0" rel="nofollow noreferrer">binary file handlers and are not appropriate for Unicode</a>. </p>
  
  <p>I need to get something to do the equivalent under Delphi 2009 and be able to handle Unicode. </p>
  
  <p>The reason why I'm opening and writing and then closing the file for each line is simply so that other programs (such as WordPad) can open the file and read it while the log is being written.</p>
  
  <p>I have been experimenting with TFileStream and TextWriter but there is very little documentation on them and few examples. </p>
  
  <p>Specifically, I'm not sure if they're appropriate for this constant opening and closing of the file. Also I'm not sure if they can make the file available for reading while they have it opened for writing.</p>
  
  <p>Does anyone know of a how I can do this in Delphi 2009 or later?</p>
  
  <hr>
  
  <p>Conclusion:</p>
  
  <p>Ryan's answer was the simplest and the one that led me to my solution. With his solution, you also have to write the BOM and convert the string to UTF8 (as in my comment to his answer) and then that worked just fine. </p>
  
  <p>But then I went one step further and investigated TStreamWriter. That is the equivalent of the .NET function of the same name. It understands Unicode and provides very clean code.</p>
  
  <p>My final code is:</p>
  
  <pre><code>procedure AppendToLogFile(S: string);
  // this function adds our log line to our shared log file
  // Doing it this way allows Wordpad to open it at the same time.
  var F: TStreamWriter;
  begin
    if LogFileName &lt;&gt; '' then begin
      F := TStreamWriter.Create(LogFileName, true, TEncoding.UTF8);
      try
        F.WriteLine(S);
      finally
        F.Free;
    end;
  end;
  </code></pre>
  
  <p>Finally, the other aspect I discovered is if you are appending a lot of lines (e.g. 1000 or more), then the appending to the file takes longer and longer and it becomes quite inefficient.</p>
  
  <p>So I ended up not recreating and freeing the LogFile each time. Instead I keep it open and then it is very fast. The only thing I can't seem to do is allow viewing of the file with notepad while it is being created. </p>