Avatar of fiveuk
fiveuk
 asked on

reading and writing a dat file to save data

I been trying to read and right info in to a dat file to hold the selected font Color,Name and Size but it dont save them or load them so am wondering if anyone can help fix my code?

[Write code]
procedure TChatForm.FormDestroy(Sender: TObject);
var
c,b,f:byte;
begin
tempstr.Free;
b:=0;
tmpstr.Clear;
tmpstr.write(FB,1);
tmpstr.write(FI,1);
b := SizeBox.ItemIndex;
f := FontBox1.ItemIndex;
c := Colour.SelectedColour;
tmpstr.write(b,1);
tmpstr.Write(f,2);
tmpstr.Write(c,3);
tmpstr.write(UserColor[1],7);
tmpstr.SaveToFile(GetProgrammDir+'Data\chatcolor.dat');
tmpstr.free;
SendStream.Free;
GetStream.Free;
end;
[/code end]

next part is the load code

[load dat code]
procedure TChatForm.FormShow(Sender: TObject);
var
b,f,c:byte;
begin
  if FileExists(GetProgrammDir+'Data\chatcolor.dat') then
   begin
    tmpstr.Clear;
    tmpstr.LoadFromFile(GetProgrammDir+'Data\chatcolor.dat');
    tmpstr.Position:=1;
    tmpstr.Read(b,1);
    tmpstr.Read(f,2);
    tmpstr.Read(c,3);
    Colour.SelectedColour:=c;
    FontBox1.ItemIndex:=f;
    SizeBox.ItemIndex:=b;
    tmpstr.Position:=2;
    tmpstr.Read(FB,1);
    tmpstr.Position:=3;
    tmpstr.Read(FI,1);
    tmpstr.Position:=4;
    tmpstr.Position:=5;
    Setlength(UserColor,7);
    tmpstr.Read(UserColor[1],7);
    tmpstr.Clear;
[end code]


hope sumone can help me cheers
Delphi

Avatar of undefined
Last Comment
Lukasz Zielinski

8/22/2022 - Mon
fiveuk

ASKER
abit more info for you  the fontbox and sizebox are both combo boxs and the color select is a dialog
mokule

what is tmpstr exactly?

before write b You're write 2 bytes,
but prior read b You set position := 1
fiveuk

ASKER
its a TMemoryStream;
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
mokule

So You're reading from quite a different positions than writing.
mokule

moreover You declared f and c as byte and trying to read 2 or 3 bytes into them
mokule

I would advice to use construction like this
  tmpstr.Write(b,sizeof(b));
and reading and writing in exactly the same order.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
fiveuk

ASKER
how would it work?
ASKER CERTIFIED SOLUTION
mokule

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
fiveuk

ASKER
that never worked it did not save the info at all

this is how dat looks   $ ¹ #000000

I need it to look like this  

    #0000FF    FontName  
TheRealLoki

you could convert it to an integer

var i: integer

i := integer(UserColor[1]);
tmpstr.Write(i, sizeof(i));

and when reading it back, do

tempstr.read(i, sizeof(i));
UserColor[1] := tcolor(i);
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Russell Libby


fiveuk,
You currently have 19 open questions, most of which experts have offered assistance which you still *need* to respond to.

Just a reminder,
Russell
Lukasz Zielinski

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup Zone:
split between: mokule {http:#17545835} and rllibby {http:#17573242}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

ziolko
Experts Exchange Cleanup Volunteer