how to save string array in memorystream to file

Hi,
I have a string array and wrote it to stream.write(myarray,sizeof(myarray));
I then did stream.savetofile('myfile.pas');
but this wont really save the contents of the array to the file and instead the file is filled with strange chars.
First time to use TMemoryStream.
Any ideas?
thanks
oalawnaAsked:
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.

Slick812Commented:
hello oalawna, ,  You do not seem to realize what you are doing, a String variable is NOT a reference to the string Data (the charaters in a string) it is a "Pointer" with a size of 4 Bytes, this string pointer is used to strore the location of the memory allocated for the characters in the string. . . so what you have when you use -
 stream.write(myarray,sizeof(myarray));
is a file with the Cardinal values (numbers) of the memory addresses of the string variables pointers, but you do NOT have any thing there that is any Text data. . . you will need to fish out the text data for each individual string of the array, , , here is a button click that will write an array of string to file -



procedure TForm1.sbut_Strings2FileClick(Sender: TObject);
const
lineBreak = #13#10;
var
aryStr: Array of String;
memStm: TMemoryStream;
i: Integer;
begin
SetLength(aryStr, 5);
aryStr[0] := '1 this is the first string !';
aryStr[1] := '2 second string.';
aryStr[2] := '3 number three string';
aryStr[3] := '4 fourth string';
aryStr[4] := '1 this is string five';
memStm := TMemoryStream.Create;
for i := 0 to High(aryStr) do
  begin
  // you can access the first charater  [1]  of the string to write to stream
  memStm.write(aryStr[i][1],Length(aryStr[i])); // you MUST get the Length of the string to get the amount to write
  memStm.write(lineBreak,2); // added a line break to make it look good
  end;
memStm.SaveToFile('E:\a text file.txt');
FreeAndNil(MemStm);
end;

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
oalawnaAuthor Commented:
thanks slick
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.