JelleH
JelleH

Reputation: 35

Reading Double data from .txt files into arrays in Delphi

I'm new to Delphi coding and struggeling in reading .txt files. I am trying to read input data (tabbed doubles) from a .txt file in which every column is regarded as a variable (Day, Temperature, Pressure,...) and every line is regarded as a time step (hour). How can i read this data into an array in order to do hourly calculations with these variables (line by line)?

Thank you so much for any advice!

Input sample (tabbed Doubles in .txt file):

1   0.5 0   -12.6   -1.39   100 -19.5   0   3.3
1   1   0   -12.6   -1.43   100 -19.8   0   3.3
1   1.5 0   -12.7   -1.51   99.9    -20.5   0   3.2

What I have so far (VCL Form application):

var              // Declaration of variables
  Read: TRead;
  i:Byte;
  data:array of array of Integer;   //Creation of dynamic array (adapts --> Setlength() command)
  Input:TextFile;
  Location:String;
  Counter:Integer;
  Maximum:Integer;

procedure TRead.Button1Click(Sender: TObject);  // Button "Read" command

begin
  Location:=Edit1.Text;                         // Path of inputfile from Form
  AssignFile(Input,(Location+'\Test1.txt'));   // Assigning inputfile
  Reset(Input);                               // Open for read-write
  If (IoResult = 0) Then Begin                 // If Inputfile reading was succesful...
  Counter:=1;
    While Not EoF(Input) Do Begin
      ReadLn(Input,i);
      Data[Counter]:=i;
      If EoF(Input) Then Break;
      Inc(Counter);       //increase 'Counter' by 1
    End;
  End

  Else WriteLn('Error when reading the file')
  CloseFile(Input);
  End;

  Begin
    For i:=1 To 10 Do WriteLn(data[i]);
    ReadLn;
  End.

Upvotes: 3

Views: 4658

Answers (2)

David Heffernan
David Heffernan

Reputation: 612844

I'd do it using TStringList to parse the file into lines and SplitString to tokenize each delimited value.

First of all to load the file into a string list:

var
  Strings: TStringList;
....
Strings := TStringList.Create;
try
  Strings.LoadFromFile(FileName);
  ProcessStrings(Strings);
finally
  Strings.Free;
end;

And then to actually process the strings:

procedure ProcessStrings(Strings: TStrings);
var
  line, item: string;
  items: TStringDynArray;
  value: Double;
begin
  for line in Strings do
  begin
    items := SplitString(line, #9#32);//use tab and space as delimiters
    for item in items do
    begin
      value := StrToFloat(item);
      //do something with value
    end;  
  end;
end;

Although your title describes the data as integer it appears to be mixed integer and floating point. Anyway, I think you should be able to fill in the blanks and populate your dynamic arrays of values, handle error checking and so on.

Upvotes: 10

Despatcher
Despatcher

Reputation: 1725

Delphi still has the very old(fashioned) pascal Read procedure for text variables so you can read in your array directly :)

Var NumArray: Array[1..9] of double; // you have 9 variables

while not eof(F) do begin
  read(F,NumArray[1],NumArray[2],NumArray[3],NumArray[4],NumArray[5],NumArray[6],NumArray[7],NumArray[8],NumArray[9]);
    // store it somewhere; 
end;

Upvotes: 5

Related Questions