• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

Saving Strings with TFileStream

how do i save strings in a fileStrem and load them again.

I have no problem doing so if i specify the length of the String like this :

var
txt : String[10]

But if i do :
var
txt :  String

it wont work.

Why does sizeof(txt) in last example return 4 ??? - no matter what ??

0
kwang080897
Asked:
kwang080897
  • 5
  • 4
  • 2
  • +1
1 Solution
 
RBertoraCommented:

sizeof is not the same as length function, which is what I think you want there.

Look at my comment for this question it might help you:
http://www.experts-exchange.com/Computers/Programming/Windows/Delphi/Q.10188486

rob.
0
 
pedeCommented:
Hi Kwang

String is quite different from string[10], because it is of dynamic lenght. It's a pointer, and that is the reason you get 4 from sizeof. Length(txt) will give you the correct size. If you use txt^ you will probably be able to write to the stream just fine.

Regards,
Pede

0
 
pedeCommented:
Hmm... Delphi won't admit that strings are pointers, so you can't use txt^.

You can do this, though, but I don't know if there is a more elegant solution:

var
   Tmp : pointer

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

Regards,
Pede

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
kwang080897Author Commented:
Hi Pede !!!
It  seems to work, how do i Load it again ????

this works (i think), but how do i get the Tmp to be a String again  ??


Stream.Read(Stringsize,sizeof(Stringsize));
Stream.Read(Tmp^,Stringsoze);


0
 
MadshiCommented:
I'm usually doing this:

  fs.Write(pointer(strVar)^,length(strVar));    // quite similar to pede's suggestion

And this:

  SetLength(strVar,fs.size);
  fs.Read(pointer(strVar)^,fs.size);

Regards, Madshi.
0
 
pedeCommented:
Hi

Yep, Madshi is right, you need to set the length with SetLength before you read from the stream. In your case use SetLength(txt2, sizeof(txt)) to read txt back into a string called txt2, and then Stream.Read(Tmp^, sizeof(Txt2));

Madshi's example will read the whole stream, as far as I can tell, not just a single string. If you don't know the length of the string you are about to read, you will have to read 1 char at a time.

/Pede

0
 
pedeCommented:
Damn, I mean Length not sizeof, in both cases!!

Somebody hit me!!

/Pede

0
 
MadshiCommented:
Hi pede, feel hit...   :-))

Yes, my example reads the whole file in one string. I'm using this function to read line by line from a stream:

function StreamRdLine(stream: TStream; var str: string) : boolean;
var c1,c2,c3 : cardinal;
    s1       : string;
begin
  result:=false;
  try
    str:='';
    c1:=stream.Size-stream.Position; result:=c1>0;
    if result then
      while c1<>0 do begin
        c2:=c1; if c2>256 then c2:=256; dec(c1,c2);
        SetLength(s1,c2); stream.Read(PChar(s1)^,c2);
        for c3:=1 to c2 do
          if s1[c3] in [#$A,#$D] then begin
            str:=str+copy(s1,1,c3-1);
            if (c3<c2) and (s1[c3]=#$D) and (s1[c3+1]=#$A) then
                 stream.Position:=cardinal(stream.Position)+c3+1-c2
            else stream.Position:=cardinal(stream.Position)+c3  -c2;
            exit;
          end;
        str:=str+s1;
      end;
  except end;
end;

Regards, Madshi.
0
 
MadshiCommented:
P.S: If you want to write your string in a single line, you should do this:

fs.Write(pointer(strVar+#$D#$A)^,length(strVar)+2);
0
 
kwang080897Author Commented:
Hi Pede and Madshi !!!

Thanks a lot to both of you, you have both been very helpfull.

If would like to reward both of you, soif one of you submit an answer, i will create a new question to the other one

Thanks again

0
 
MadshiCommented:
Kwang, that's fair to reward both of us...  :-)

Pede, please answer this one. I'll take the other one...
0
 
pedeCommented:
Sure thing

0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now