Okay... got the demo working... took some time, lots of documentation telling me what everything is "methods etc etc, BUT no Code examples.
After a few hours I finally got this to work.
It may be crude, please feel free to make it better.
On in this example, I have a field text that has two fields separated by a "|" delimiter.
Then I use TStrngList to separate the data using the delimiter "^".
Therefore I can store lots of different values using stream in a text file without using binary.
FYI... I do have a working demo of binary too. But I wanted to workout the TFileStream for just plain text.
1) Single value text box
2) Delimited value string "This is two values,^in a test string" (to read in 2 separate values)
3) Memo string (including commas)
Here is my file ouput.
test.txt
Edit1|Edit2|This is two values,^in a test string|a 3rd text string that is a MEMO text control.
|
Here is my code...
WRITE:
procedure TForm1.Button1Click(Sender: TObject);
var
strm: TFileStream;
n1: longint;
n2: longint;
n3: longint;
n4: longint;
txt1: string;
txt2: string;
txt3: string;
txt4: string;
fname: String = 'test.txt';
begin
strm := TFileStream.Create(fname, fmCreate);
txt1:= Edit1.Text+'|';
txt2:= Edit2.Text+'|';
txt3:= Edit3.Text+'|';
txt4:= Memo1.Text+'|';
n1 := Length(txt1);
n2 := Length(txt2);
n3 := Length(txt3);
n4 := Length(txt4);
try
strm.Position := 0;
strm.Write(txt1[1], n1);
strm.Write(txt2[1], n2);
strm.Write(txt3[1], n3);
strm.Write(txt4[1], n4);
finally
strm.Free;
end;
READ:
procedure TForm1.Button2Click(Sender: TObject);
var
strm: TFileStream;
txt:String;
sl1: TStringlist;
sl2: TStringlist;
spltxt: String;
lng: LongInt;
fname: String = 'test.txt';
begin
txt:='';
try
strm:= TFileStream.Create(fname, fmOpenRead);
SetLength(txt, lng);
strm.Read(txt[1], lng);
finally
strm.Free;
end;
sl1:= TStringList.Create;
try
sl1.StrictDelimiter:= True ;
sl1.Delimiter:='|';
sl1.DelimitedText:=txt;
Edit1.Text:=sl1[0];
Edit2.Text:=sl1[1];
spltxt:=sl1[2];
Edit3.Text:=spltxt;
Memo1.Text:=sl1[3];
finally
sl1.Free;
end;
sl2 := TStringList.Create;
try
sl2.StrictDelimiter := True ;
sl2.Delimiter:= '^';
sl1.DelimitedText := spltxt;
Edit4.Text:=sl2[0];
Edit5.Text:=sl2[1];
finally
sl2.free
end;
end;
Thanks for your inputs!!