Re: data from text extraction
Posted by WAllison on December 06, 2001:
In Reply to: Re: data from text extraction posted by Rob Pouwelse on November 08, 2001:
: : I've got a question..:
: : how can i extract certain values (int's or string's) from a text file??
: : it's like this:
: : now i have to extract certain values (the ones behind the "=" but not all)
: : I haven't the foggiest on how to do it..
: : please help..
: : tnx, in advance
I quickly came up with this - this way you need to know the keynames before the =.
GetDataString('F:\Source\TestApp\TestFile.txt', 0, 'from');
If i get time ill write one that goes through th file picking them automatically.
function TMainForm.GetDataString(FileName: string; LineNo: Integer; KeyName: string): string; var F: TextFile; i, APos, BPos, CPos: integer; s, s1: string; begin If KeyName[Length(KeyName)] <> '=' Then KeyName := KeyName + '='; If FileExists(FileName) Then begin AssignFile(F, FileName); Reset(F); i := 0; while not (EOF(F)) and (i <> LineNo + 1) do begin Readln(F, s); i := i + 1; end; APos := Pos(Uppercase(KeyName), UpperCase(s)); if APos > 0 Then begin APos := APos + Length(KeyName); s1 := Copy(s, APos, Length(s) - APos); BPos := Pos(',', s1); If BPos = 0 Then BPos := Length(s); CPos := APos + Pos(',', s1); s := Copy(s, APos, BPos - 1); end else s := ''; CloseFile(F); end; Result := s; end;