Saving a string to a StreamFile

I am writing a code to save several strings to a StreamFile.
The strings are defined as (example):

Title:string[20];

to save to StreamFile I use:

BgtFile.WriteBuffer(Title[1],SizeOf(Title));

I have also tried:

BgtFile.WriteBuffer(Title[0],SizeOf(Title));

And both work, most of the time.
The problem is that some times the Length Byte is saved as zero (the string is correctly saved as checked with a Hex editor) and so when I read the string back to the variable "Title", I get an empty string.

Any idea why this is happening?

Thanks.

Comments

  • : I am writing a code to save several strings to a StreamFile.
    : The strings are defined as (example):
    :
    : Title:string[20];
    :
    : to save to StreamFile I use:
    :
    : BgtFile.WriteBuffer(Title[1],SizeOf(Title));
    :
    : I have also tried:
    :
    : BgtFile.WriteBuffer(Title[0],SizeOf(Title));
    :
    : And both work, most of the time.
    : The problem is that some times the Length Byte is saved as zero (the
    : string is correctly saved as checked with a Hex editor) and so when
    : I read the string back to the variable "Title", I get an empty
    : string.
    :
    : Any idea why this is happening?
    :
    : Thanks.
    :
    Delphi strings are complex structures, to which the variable is a pointer. In this case SizeOf() will return the size of that pointer. The solution is to use Length() instead. The correct code is:
    [code]
    BgtFile.WriteBuffer(Title[1], Length(Title));
    [/code]
  • Hi.

    I also have a similar problem...
    Found an example on Torry's Delphi pages, but cant get it to work.
    The code here:
    *************************
    type
    TPerson = record
    Name: string[50];
    Address: string[50];
    end;

    var
    Form1: TForm1;
    person:tperson;
    Stream: TFileStream;

    implementation

    {$R *.nfm}

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    try
    Stream := TFileStream.Create('test.dat', fmOpenreadwrite);
    except
    Stream := TFileStream.Create('test.dat', fmCreate);
    end;
    person.name:='Any name';
    person.address:='Any address';
    Stream.WriteBuffer(Person, sizeof(TPerson));
    Stream.free;
    end;
    *********************
    When I run this, I get a error from Delphi saying:
    There is no overloaded version of 'Writebuffer' that can be called with these arguments.

    Any ideas anyone?
    I have tried to switch from SizeOf to Length...

    Roald
  • : Hi.
    :
    : I also have a similar problem...
    : Found an example on Torry's Delphi pages, but cant get it to work.
    : The code here:
    : *************************
    : type
    : TPerson = record
    : Name: string[50];
    : Address: string[50];
    : end;
    :
    : var
    : Form1: TForm1;
    : person:tperson;
    : Stream: TFileStream;
    :
    : implementation
    :
    : {$R *.nfm}
    :
    : procedure TForm1.Button1Click(Sender: TObject);
    : begin
    : try
    : Stream := TFileStream.Create('test.dat', fmOpenreadwrite);
    : except
    : Stream := TFileStream.Create('test.dat', fmCreate);
    : end;
    : person.name:='Any name';
    : person.address:='Any address';
    : Stream.WriteBuffer(Person, sizeof(TPerson));
    : Stream.free;
    : end;
    : *********************
    : When I run this, I get a error from Delphi saying:
    : There is no overloaded version of 'Writebuffer' that can be called
    : with these arguments.
    :
    : Any ideas anyone?
    : I have tried to switch from SizeOf to Length...
    :
    : Roald
    :
    WriteBuffer() probably expects the data as a pointer. You should add the @-operator to get the address of Person. See help files for the @-operator.
    Please next time use the [ code ] style-code, when posting code. For more info on style-codes, see below the entry box in programmersheaven.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories