Reading UTF8 strings from a file lazarus / 61-Beginners ---------------------------------------------------------------------- Nitorami · 2022-01-24 14:03 Hi all The attached file is a raw file generated by LTSpice, an electronic circuit simulator. The file contains a header using UTF8 characters, followed by the binary data after the word "Binary:". I simply need to read a fews keywords and information from the header, but I do not know how to do this efficiently, because I am unexperienced with UTF strings. With old style 8-bit ASCII strings, I would simply use readln (), but how to do this with UTF8? I tried TFileStream.ReadAnsiString but it does not work because the characters seem to be stored as raw UTF8chars without length encoding. Can someone give me a bit of starting aid please? ---------------------------------------------------------------------- engkin · 2022-01-24 15:22 The file is UTF16, not UTF8. ---------------------------------------------------------------------- wp · 2022-01-24 16:15 Tested this to work: 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 procedure TForm1.Button1Click(Sender: TObject);var  stream: TFileStream;  buffer: UnicodeString = '';  search: UnicodeString = 'Binary:';  p: Integer;  s: String = '';                 begin  stream := TFileStream.Create('d:\Download\test_circuit.raw', fmOpenRead);  try  // Read the file into a single UFT-16 string. The length of the string is     // half of the file size (2 bytes per UTF-16 "character").    SetLength(buffer, stream.Size div SizeOf(wideChar));    stream.Read(buffer[1], stream.Size);    // Find the position of the unicode word "Binary:"    p := pos(search, buffer);    if p > 0 then    // Get the string up to the found position and convert to UTF-8...      s := Copy(buffer, 1, p-1)    else      s := '(not found)';    // ... and assign to a memo    Memo1.Lines.Text := s;  finally    stream.Free;  end;end; ---------------------------------------------------------------------- Nitorami · 2022-01-24 16:54 Thanks WP, that works. I am surprised that the old pos() function works on widestrings as well, while readln() doesn't. I had started myself to scan through the myriads of functions in unit strutils, and found a few which are similar to pos() - e.g. "containsstr","searchbuf", but if pos() works, I will use that. I understand I cannot read the file line by line using readln, correct ? I would have to write my own readline(), e.g. in a TFileStream descendant. The function would read the file into a buffer word by word until it finds (widestring) EOL, and then copy the buffer to a string, or stringlist. I wonder why such a function does not seem to exist already. ---------------------------------------------------------------------- SymbolicFrank · 2022-02-18 15:34 The Text property of a Delphi-compatible TStringList would do that.