AssignFile/Rewrite saves text to a file exactly as given in the code below.
When FileStream is used to save the same text to a file, it adds 4 bytes at the begining (HEX: 0C 00 00 00).
program writeFileTest;
{$mode objfpc}{$H+}
uses
Classes, sysutils;
var
fs: TFileStream;
s: String;
tf: TextFile;
begin
s := 'Hello world.';
AssignFile(tf, ExtractFilePath(ParamStr(0)) + 'file1.txt');
Rewrite(tf);
Write(tf, s);
CloseFile(tf);
fs := TFileStream.Create(ExtractFilePath(ParamStr(0)) + 'file2.txt', fmCreate);
fs.WriteAnsiString(s);
fs.Free;
end.
file1.txt HEX: 48 65 6C 6C 6F 20 77 6F 72 6C 64 2E
file2.txt HEX: 0C 00 00 00 48 65 6C 6C 6F 20 77 6F 72 6C 64 2E
My first guess is that's some encoding information for text files on linux, but I'm not sure.
"file" command in console gives me following information:
$ file -bi file1.txt
text/plain; charset=us-ascii
$ file -bi file2.txt
application/octet-stream; charset=binary
Is there a way to change how TFileStream saves files (to be the same as AssignFile/Rewrite works)?