Writing strings to TFileStream?

How do i write a string into a TFileStream?

I have an initiated filestream (fsKeyStream) var passed to a method in a key object containing some strings i need to save.

procedure TKey.SaveToFile(var fs : TFileStream );
begin
  fs.write(Title, sizeof(Title);
  ..
  ..
end;

procedure TKeyList.SaveToFile;
var
  keystream: TFileStream;
begin
  keystream := TFileStream.Create(...
  .. go through list items and...
    TKey(Items[index]).SaveToFile( keystream );
...
end;

The strings in Key contains the right data when i debug, and the resulting file has the right amount om items saved to it, but the saved strings are just empty..

What do i do wrong?

Micappel
micappelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RBertoraCommented:
you need to do something like this:



var
   Tmp : pointer

   Tmp := pointer(txt);
   YourFileStream.Write(Tmp^, Length(txt));

or declare similar:

txt : String[20]

Rob ;-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MadshiCommented:
Rob is right. This is the same in shorter form...   :-)

  YourFileStream.Write(pchar(txt)^, Length(txt));

If you want to write more than one string into a file you should either write a #$D#$A behind each string (then you have a text file with line breaks after each string) or you should write the length of the string in front of the characters (then you have a binary file). A text file is perhaps nicer, but a binary file is faster to read.

Text file:

  YourFileStream.Write(pchar(txt + #$D#$A)^, Length(txt) + 2);

Binary file:

var i1 : integer;
 
  i1 := Length(txt);
  YourFileStream.Write(i1, 4);
  YourFileStream.Write(pchar(txt)^, i1);

Regards, Madshi.
0
micappelAuthor Commented:
Thanks!

Then, how do i read it back? Do i have to reference it as a pointer there to?
0
RBertoraCommented:
I think you do something like this:

you can declare something like

Buffer : String[200];

 Read(Buffer,200);

or

BufferPointer : Pointer;

BufferPointer := allocmem(200);
Read(bufferPointer,200);

But I never have used filestreams my comments are purely theoretical... so perhaps madshi can confirm.
Rob ;-)

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.