Strange bytes when using TFileStream on Linux lazarus / 62-General ---------------------------------------------------------------------- dogriz · 2020-09-25 07:45 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). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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. Code: [Select]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: Code: [Select]$ file -bi file1.txt text/plain; charset=us-ascii Code: [Select]$ 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)? ---------------------------------------------------------------------- hansotten · 2020-09-25 08:02 Looks like the length is also stored in the stream. (0c 00 00 00 = 12, the text has 12 chars). Length is in shortstrings and ansistrings, https://wiki.lazarus.freepascal.org/Character_and_string_types ? TfileStream is not special for text files, so stores the variable as is, with length. it is a binary file, without line ends. write on a text file stores the string as a text line. because you did not use a writeln the line ending is not in the text file. If you want  a textfile with tfilestream you should feed it characters and line endings, not just a string ---------------------------------------------------------------------- lucamar · 2020-09-25 08:14 It's written in the docs about TStream (the base class): QuoteWriteAnsiString writes the AnsiString S (i.e. 4 bytes) to the stream. This is a utility function which simply calls the Write function. The ansistring is written as a 4 byte length specifier, followed by the ansistring's content. The ansistring can be read from the stream using the ReadAnsiString function. To emulate Assign/Rewrite/Write you have to use Stream.Write or Stream.WriteBuffer: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   fs := TFileStream.Create(ExtractFilePath(ParamStr(0)) + 'file2.txt', fmCreate);  fs.WriteBuffer(s[1], Length(s));  fs.Free; ---------------------------------------------------------------------- rvk · 2020-09-25 08:16 You could do this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i fs := TFileStream.Create(ExtractFilePath(ParamStr(0)) + 'file2.txt', fmCreate);fs.WriteBuffer(Pointer(s)^, length(s));fs.Free; Note: you used Write(tf, s) so this example with WriteBuffer also only writes the string, not a CR+LF (or CR) after it. Reading would be more difficult because you can't use readln and you would need to recognize CR. If you want to handle longer files with strings you might want to use TStringList, TStringStream etc. ---------------------------------------------------------------------- dogriz · 2020-09-25 08:48 Thanks all for clarification. TFileStream.WriteBuffer gives me what I expeted. ---------------------------------------------------------------------- hansotten · 2020-09-25 09:18 Quote from: dogriz on September 25, 2020, 10:48:59 amThanks all for clarification. TFileStream.WriteBuffer gives me what I expeted. fs.WriteBuffer(s[1], Length(s)); Yes, you skip the length being written this way. But the result not a text file. You still have to think about line endings if you want the equivalent of writeln  to a textfile if that is what you want. ---------------------------------------------------------------------- rvk · 2020-09-25 09:25 writeln() equivalent would be Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i s := s + sLineBreak;fs.WriteBuffer(s[1], length(s)); Reading would be much harder ---------------------------------------------------------------------- lucamar · 2020-09-25 11:00 Quote from: hansotten on September 25, 2020, 11:18:45 amYou still have to think about line endings if you want the equivalent of writeln  to a textfile if that is what you want. It's still a text file, it just have a single neverending story line. That's, among other reasons, why TStream.WriteAnsiString() and  TStream.ReadAnsiString() write the string's length before its contents, to be able to write/read the string as it is/was. ---------------------------------------------------------------------- PascalDragon · 2020-09-25 13:12 Quote from: hansotten on September 25, 2020, 11:18:45 amYes, you skip the length being written this way. But the result not a text file. You still have to think about line endings if you want the equivalent of writeln  to a textfile if that is what you want. Of course the result is a text file. If I store a file in Notepad that only contains Hello World without any trailing line ending it will have the same data. (If you want to write multi line text however then you need to make sure that line endings are written as well, however)