Mady Daby
asked on
How do you write arrays to a textfile?
var TimeTableArray : Array [1..28,1..288] of String;
Procedure InitialiseTimeTable;
Var i , j : integer;
Begin
for i := 1 to 28 do
Begin
for j := 1 to 288 do
Begin
TimeTableArray[i,j] := ' ';
End;
End;
End;
Procedure WriteTimeTableDataToFile;
var tdata : textfile;
i, j : integer;
Begin
for i := 1 to 28 do
begin
assignfile(tdata, UserPath + inttostr(i) +'.txt');
rewrite(tdata);
for j := 1 to 288 do //This loop causes the error
Begin //I/O error 105
writeln(TimeTableArray[i,j ]);
End;
closefile(tdata);
end;
End;
It works fine if the loop above only writes ' '
Procedure InitialiseTimeTable;
Var i , j : integer;
Begin
for i := 1 to 28 do
Begin
for j := 1 to 288 do
Begin
TimeTableArray[i,j] := ' ';
End;
End;
End;
Procedure WriteTimeTableDataToFile;
var tdata : textfile;
i, j : integer;
Begin
for i := 1 to 28 do
begin
assignfile(tdata, UserPath + inttostr(i) +'.txt');
rewrite(tdata);
for j := 1 to 288 do //This loop causes the error
Begin //I/O error 105
writeln(TimeTableArray[i,j
End;
closefile(tdata);
end;
End;
It works fine if the loop above only writes ' '
Where are you filling the array with something other than ' '?
mlmcc
mlmcc
use the csv file idea ...
you'll have 28 lines with 288 items per line
fwiw, you are recreating the file per line
the place of your rewrite should be outside the loop
to read back in :
you'll have 28 lines with 288 items per line
var
i, j: integer;
List: TStrings;
begin
assignfile(tdata, UserPath + inttostr(i) +'.txt');
rewrite(tdata);
try
List := TStringList.Create;
try
List.StrictDelimiter := True;
for I := 1 to 28 do
begin
List.Clear;
for J := 1 to 288 do
List.Add(TimeTableArray[i,j]);
WriteLn(tData, List.CommaText);
end;
finally
List.Free;
end;
finally
closefile(TData);
end;
end;
fwiw, you are recreating the file per line
the place of your rewrite should be outside the loop
to read back in :
ReadLn(Tdata, S);
List.CommaText := S;
for J := 1 to 288 do
TimeTableArray[i,j] := List[J-1];
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
writeln(tdata, TimeTableArray[i,j]);
You have to tell the program to write into the file opened, not the standard output.
Hope this helps
Manuel