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
LVL 2
kjtengAsked:
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.

rwilson032697Commented:
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...)

Cheers,

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

var
  F: TextFile;
begin
  AssignFile(F, 'Text.tst');
  Rewrite(F);
  Writeln(F, SS);
  CloseFile(F);
end;

Motaz

www.geocities.com/motaz1
0
MadshiCommented:
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.
0
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.

MadshiCommented:
Or this works, too:

FileStream.write(pointer(longStrVar)^, Length(SS));
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:
Aaaahh - of course I meant "Length(longStrVar)"!

Sorry for posting 3 comments instead of 1...   :-)
0
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.
0
MotazCommented:
I see kjteng, and thanks for your comment.
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.