Re: data from text extraction


[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]

Posted by WAllison on December 06, 2001:

In Reply to: 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:
: Nov 5 09:00:00 gateway newsyslog[14491]: logfile turned over //this is the first line
: Nov 5 09:02:15 gateway sendmail[14502]: JAA14502: from=...
: ....

 

: now i have to extract certain values (the ones behind the "=" but not all)
: The lines are random in order...

: I haven't the foggiest on how to do it..
--------------------------

//no frills - dunno how reliable this one is - but it gives u an idea of how to approach the problem.

function TMainForm.GetDataStrings(FileName: string; DataList: TStringList): boolean;
var F: TextFile;
    APos, BPos: Integer;
    s, s1: string;
begin
  Result := False;
  AssignFile(F, FileName);
  Reset(F);
  While not EOF(F) do
   begin
    ReadLn(F, s);
    APos := Pos('=', s);
    While (s[APos] <> ' ') do APos := APos - 1;
    APos := APos + 1;
    s := copy(s, APos, Length(s));
    While Length(s) > 0 do
     begin
      BPos := Pos(',', s);      //Copy the data
      s1 := copy(s, 0, BPos - 1);
      DataList.Add(s1);        //Remove the first entry
      delete(s, 1, BPos + 1);
     end;
    Result := DataList.Count > 0;
   end;
  CloseFile(F);
end;

Related Articles and Replies


[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]