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.
Delphi still has the very old(fashioned) pascal Read procedure for text variables so you can read in your array directly 🙂