writing long string type variable

What is the correct way to write a long string type variable SS to a file

This works:
SS := '123456khjk';
FileStream.write(SS[1], Length(SS))

This works too:
SS := '12345678';
StrAlloc(pp, 255);
pp := Pchar(SS);  //pp is a pchar
FileStream.Write(pp^, Length(SS))

This one doesn't work.
FileStream.write(SS, Length(SS)) //Why
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.

The first two ways are OK, but you may also need to write the length of the string to the stream, so that you know how much to read if you read it again.

The third example fails because you are attempting to write out the value of the pointer rather than the data the pointer points two (huge string types are effectively pointers...)


It is better if you write it in text file, it is very easy and it does not require write string length. Example:

  F: TextFile;
  AssignFile(F, 'Text.tst');
  Writeln(F, SS);


My prefered method is this one:

FileStream.write(pchar(longStrVar)^, Length(SS));

It has the advantage that you don't get an exception when the string is empty. (In D4&5 you get an exception if you use longStrVar[1] on an empty string, in D3 not).

Regards, Madshi.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Or this works, too:

FileStream.write(pointer(longStrVar)^, Length(SS));

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
Aaaahh - of course I meant "Length(longStrVar)"!

Sorry for posting 3 comments instead of 1...   :-)
kjtengAuthor Commented:
Thanks for all your ans/comment. I will accept Madshi's comment as answer. I definitely know that ss in my third example is a pointer. I tried to write ss^  but it did not work. Madshi's suggestion [ Pointer(longStrVar)^ ] is  exactly what I want.

Motaz: I cannot use text file. My example has been simplified. I am actually dealing with untype binary files and ss is only one of the fields to be written.
I see kjteng, and thanks for your comment.
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

From novice to tech pro — start learning today.