Link to home
Start Free TrialLog in
Avatar of Stelios_Antoniou
Stelios_Antoniou

asked on

Delphi UTF-8 strings inside records

I use a record like this...

type
  TNameRec = record
    Nam: string[maxNameLength];
    Var : real;
    Siz : integer;
  end;

to store and access data to files of class TRecordStream = class(TFileStream)

using functions like...

var
    fileStream: TRecordStream;
begin
    fileStream.ReadNameRec(NameRec);
    fileStream.writeNameRec(NameRec);
end;

declared as...

function TRecordStream.WriteNameRec (Rec: TNameRec  ): Longint;
begin
  // This function writes the record Rec to the stream
  Result := Write(Rec, sizeOf(Rec));
end;

function TRecordStream.ReadNameRec (var Rec: TNameRec  ): Longint;
begin
  // This function reads the record Rec from the stream
  Result := Read(Rec, sizeOf(Rec));
end;

This works fine for me. The problem rises when i want to save utf-8 strings.
In this case the strings cannot be stored properly (uknown characters), unless I remove the declaration of constant length for the strings as below:

type
  TNameRec = record
    Nam: string;    //[maxNameLength];
    Var : real;
    Siz : integer;
  end;

if i do that, the utf-8 strings can be saved and then be accessed keeping their actual characters. Unfortunately, when i try to read these records from the filestream, sometimes the process works fine, and sometimes there is access failure, obviously due to miscalculation of the record length.

Is there a way to save and read these utf-8 strings properly, but by keeping this record structure? Of course, if it's not possible, any solution close to this is welcomed.
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Stelios_Antoniou
Stelios_Antoniou

ASKER

Thank you for your answer

I have compile problem with the line

ZeroMemory(@rName, SizeOf(TNameRec));

Shouldn't I define this procedure? And what is '@rName'?
Sorry, this is part of my global test variable. You use own rec. var. ZeroMemory is useful to sell all bytes/record to zero. Just remove this.
It's working !

Thanks