Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Saving Strings with TFileStream

Posted on 1999-07-29
12
Medium Priority
?
202 Views
Last Modified: 2010-04-04
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
Comment
Question by:kwang080897
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 7

Expert Comment

by:RBertora
ID: 1391851

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
 
LVL 1

Expert Comment

by:pede
ID: 1391852
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
 
LVL 1

Expert Comment

by:pede
ID: 1391853
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:kwang080897
ID: 1391854
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1391855
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
 
LVL 1

Expert Comment

by:pede
ID: 1391856
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
 
LVL 1

Expert Comment

by:pede
ID: 1391857
Damn, I mean Length not sizeof, in both cases!!

Somebody hit me!!

/Pede

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1391858
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1391859
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
 

Author Comment

by:kwang080897
ID: 1391860
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1391861
Kwang, that's fair to reward both of us...  :-)

Pede, please answer this one. I'll take the other one...
0
 
LVL 1

Accepted Solution

by:
pede earned 200 total points
ID: 1391862
Sure thing

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question