Re: reading text files with Delphi

Posted by Nancy on January 14, 2005
In Reply to: reading text files posted by tristan on January 14, 2005

: just wondering i have a voting form, where a user can select what they want i.e. whats their favourite color, red, green, blue etc and my delphi program saves this to a text file.

: my question is how do i then get delphi to read the text file (vote.text), and state red = 3, green = 13, blue = 30 votes etc.
-------------------------------------------

If each category of votes is on a single line, it's easy: create a StringList, say SLVotes and do a LoadFromFile.

Have variables: for each category, an indexing variable, and so on, like this:

var
  Red, Green, Blue, i, P: integer;
  S: string; 

Traverse the stringlist SLVotes, extract the votes and put them in variables:

for i := 0 to SLVotes.Count - 1 do begin
  S := SLVotes[i];
  P := Pos('=', S);
  if Pos('red', S) > 0 then
    Red := StrToInt(Copy(S, P+1, 100)
  else if Pos('green', S) > 0 then
    Green := StrToInt(Copy(S, P+1, 100)
  else ... // and so on
end;

Delphi Forum and Tutorials