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(Send er: 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(GetProgr ammDir+'Da ta\chatcol or.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\chat color.dat' ) then
begin
tmpstr.Clear;
tmpstr.LoadFromFile(GetPro grammDir+' Data\chatc olor.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
[Write code]
procedure TChatForm.FormDestroy(Send
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],
tmpstr.SaveToFile(GetProgr
tmpstr.free;
SendStream.Free;
GetStream.Free;
end;
[/code end]
next part is the load code
[load dat code]
procedure TChatForm.FormShow(Sender:
var
b,f,c:byte;
begin
if FileExists(GetProgrammDir+
begin
tmpstr.Clear;
tmpstr.LoadFromFile(GetPro
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
what is tmpstr exactly?
before write b You're write 2 bytes,
but prior read b You set position := 1
before write b You're write 2 bytes,
but prior read b You set position := 1
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.
tmpstr.Write(b,sizeof(b));
and reading and writing in exactly the same order.
ASKER
how would it work?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
this is how dat looks $ ¹ #000000
I need it to look like this
#0000FF FontName
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);
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER