Board index » delphi » Q: Saving string content to a file?

Q: Saving string content to a file?

Hi All,

I am trying to save the content of a (long) string to a file.
Basically what I tried is:

var
  theFile: TFileStream;
  theFileName, theContent: string;
begin
  theFileName:= 'MyFile.dat';
  theContent:= #0#1#2#3#4#5#6#7#8#9;
  theFile:= TFileStream.Create(theFileName, fmCreate or
fmShareDenyWrite);
  theFile.WriteBuffer(theContent, length(theContent));
  theFile.Free;
end;

Content of saved file is not the same as content of string.
Anyone knows how should I present the string to WriteBuffer?
-----
  [To reply with e-mail to me, delete the letter 'd' from my name]

 

Re:Q: Saving string content to a file?


In article <3552F8F9.5913B...@clic.nl>,
  "k. djakman" <kem...@clic.nl> wrote:

Quote

> Hi All,

> I am trying to save the content of a (long) string to a file.
> Basically what I tried is:

> var
>   theFile: TFileStream;
>   theFileName, theContent: string;
> begin
>   theFileName:= 'MyFile.dat';
>   theContent:= #0#1#2#3#4#5#6#7#8#9;
>   theFile:= TFileStream.Create(theFileName, fmCreate or
> fmShareDenyWrite);
>   theFile.WriteBuffer(theContent, length(theContent));
>   theFile.Free;
> end;

> Content of saved file is not the same as content of string.
> Anyone knows how should I present the string to WriteBuffer?

        The string is a pointer, and you're writing the _value_
of that pointer to the file, which is not what you want.
You should get what you want if you say

theFile.WriteBuffer(theContent[1], length(theContent));

instead. (Of course if you're planning on reading this file
someday probably you should first write the string's
length to the file so you can read it to decide how many
characters to read into the string.)

David C. Ullrich

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading

Other Threads