Delphi: Strings in Records bigger than 255 chars

delphi, record, string

Solution

If you want to be able to write your record to a file, you can define your string as an array of ansichar for example. You can treat it like a string afterwards.

Example:

program StrInRecTest;
{$APPTYPE CONSOLE}
uses SysUtils;

type
  TStringRec=
    packed record
        S:array[0..1023] of ansichar;
    end;

var
  StringRec:TStringRec;
  F:File of TStringRec;
begin
  StringRec.S := 'Hello';
  WriteLn(StringRec.S);
  WriteLn('First char is:'+StringRec.S[0]); // watch out with this


  // now let's try saving this to a file and reading it back...

  // make a long string with x-es
  FillChar(StringRec.S,Length(StringRec.S),'X');
  StringRec.S[High(StringRec.S)] := #0; // terminate the string

  WriteLn(StringRec.S);

  // write to a file
  AssignFile(F,'tmp.txt');
  ReWrite(F);
  Write(F,StringRec);
  CloseFile(F);

  WriteLn;

  // read from file
  AssignFile(F,'tmp.txt');
  Reset(F);
  Read(F,StringRec);
  CloseFile(F);

  WriteLn(StringRec.S); // yay, we've got our long string back

  ReadLn;
end.

Problem

Is there a way to get strings in records bigger than 255 chars? EDIT: I have something like the following: ``` TQuery = Record Action: string[255]; Data: string; end; ``` if I now say: ``` Test: TQuery; Test.Data := 'ABCDEFGHIJKLMN...up to 255...AISDJIOAS'; //Shall be 255 chars ``` It does not work and the compiler complains... How to fix that?

Original source