I am trying to save a line for each event containing a piece of text and the time + date when happened.
The problem is:
- time is shown as Chinese font
- it replaces the same line over and over again
Here is the code:
uses sysUtils, classes;
function log: Boolean;
var
fs: TFileStream;
i : String;
time : TDateTime;
begin
i := 'Boss is dead!';
time := now;
try
fs := TFileStream.Create('log.txt', fmCreate or fmOpenWrite);
fs.Write(PChar(i +TimeToStr(time))^, Length(i +TimeToStr(time)));
fs.write(i, sizeof(i));
finally
fs.Free;
end;
end;
Thank you.
Usually when you expect Latin text, but see Chinese text, that means that you are interpreting ANSI text as though it were UTF-16. From this I infer that you are intending to write out UTF-16 text, but are actually writing out ANSI text. Which means that you have a pre-Unicode Delphi.
As far as why you keep overwriting the file, then that's because you pass
fmCreate. What you really want to do is open an existing file in write mode, or if no file exists, create a new file. The Win32 API function supports that with theOPEN_ALWAYScreation disposition. But that is not available throughTFileStream.Create. So you have to useTHandleStreamand callCreateFiledirectly. Note that newer versions of Delphi introduceTFile.Openthat exposesOPEN_ALWAYSfunctionality. But my reasoning says you are using a Delphi that is too old.Assuming that you can use
THandleStream, and callCreateFilecorrectly, then you just need to write out UTF-16 text. The simplest way is to use aWideString. You might write the code like this:On the other hand, perhaps the Chinese comes from here:
This code simply writes a pointer to the file. You for sure don't want to do that and should remove that line. For sake of completeness, if you do have a Unicode Delphi, then you would write the text in a quite similar way. Like this:
Going back to appending to a stream, that would run like this:
Finally, I have had to make a lot of guesses about your environment in this answer. In future, always include the Delphi version that you use. And always include a clear statement of your goals.