Link to home
Start Free TrialLog in
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
Avatar of fiveuk
fiveuk

ASKER

abit more info for you  the fontbox and sizebox are both combo boxs and the color select is a dialog
what is tmpstr exactly?

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

ASKER

its a TMemoryStream;
So You're reading from quite a different positions than writing.
moreover You declared f and c as byte and trying to read 2 or 3 bytes into them
I would advice to use construction like this
  tmpstr.Write(b,sizeof(b));
and reading and writing in exactly the same order.
Avatar of fiveuk

ASKER

how would it work?
ASKER CERTIFIED SOLUTION
Avatar of mokule
mokule
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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  
Avatar of 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);
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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