Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 589547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:26:07+00:00 2026-05-13T15:26:07+00:00

I had some code before I moved to Unicode and Delphi 2009 that appended

  • 0

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:

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 <> '' then begin
    F := CreateFileA(Pchar(LogFileName), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_ALWAYS, 0, 0);
    if F <> 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;

But CreateFileA and WriteFile are binary file handlers and are not appropriate for Unicode.

I need to get something to do the equivalent under Delphi 2009 and be able to handle Unicode.

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.

I have been experimenting with TFileStream and TextWriter but there is very little documentation on them and few examples.

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.

Does anyone know of a how I can do this in Delphi 2009 or later?


Conclusion:

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.

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.

My final code is:

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 <> '' then begin
    F := TStreamWriter.Create(LogFileName, true, TEncoding.UTF8);
    try
      F.WriteLine(S);
    finally
      F.Free;
  end;
end;

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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:26:08+00:00Added an answer on May 13, 2026 at 3:26 pm

    For logging purposes why use Streams at all?

    Why not use TextFiles? Here is a very simple example of one of my logging routines.

    procedure LogToFile(Data:string);
    var
      wLogFile: TextFile;
    begin
      AssignFile(wLogFile, 'C:\MyTextFile.Log');
      {$I-}
      if FileExists('C:\MyTextFile.Log') then
        Append(wLogFile)
      else     
        ReWrite(wLogFile); 
      WriteLn(wLogfile, S);
      CloseFile(wLogFile);
      {$I+}
      IOResult; //Used to clear any possible remaining I/O errors 
    end;
    

    I actually have a fairly extensive logging unit that uses critical sections for thread safety, can optionally be used for internal logging via the OutputDebugString command as well as logging specified sections of code through the use of sectional identifiers.

    If anyone is interested I’ll gladly share the code unit here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 456k
  • Answers 456k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Since those return statements you currently have don't run until… May 15, 2026 at 10:27 pm
  • Editorial Team
    Editorial Team added an answer Each UIViewController already has a property navigationController for this use.… May 15, 2026 at 10:27 pm
  • Editorial Team
    Editorial Team added an answer You don't need to use the underline, it's just a… May 15, 2026 at 10:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.