Reputation: 23
I am reading a text file into a record array. Part of the data in the text files are program grades and test grades. Can I declare an array of integers to fold the program or test values as a field in the record? If so, then how do I access the individual program values in the field? For example, could I declare the record like this? And if so how would I input or access data from say test[3]?
nametype = record
first : string[10];
mi : string[3];
last : string[30];
end;
stype = record
id : integer;
name : nametype;
prog : array[1..10] of integer;
test : array[1..3] of integer;
progave, quizave : real;
average : double;
grade : char;
end;
sarraytype = array[1..100] of stype;
var
student : sarraytype;
So I guess where I'm stuck is reading into these arrays. So far for my read I have:
procedure TstudentData.openButtonClick(Sender: TObject);
begin
var i : integer;
if open.execute then
begin
assignfile(inf,open.FileName);
reset(inf);
i := 1;
while not eof(inf) do with student[i] do
begin
readln(inf, id, name.first, name.mi, name.last);
i := i + 1;
end;
end;
i:=1;
end;
I have the read for the other data, but I'm at a total loss for how to read into the prog and test arrays.
Upvotes: 1
Views: 1042
Reputation: 27385
You can. By an example ...
var
a: sarraytype;
i, j: integer;
begin
for i := low(a) to High(a) do
for j := low(a[i].test) to High(a[i].test) do
a[i].test[j] := i * 100 + j;
for i := low(a) to High(a) do
for j := low(a[i].test) to High(a[i].test) do
Memo1.Lines.Add(IntToStr(a[i].test[j]));
end;
as response to your comment
var
a,b: sarraytype;
i, j: integer;
fs:TFileStream;
begin
for i := low(a) to High(a) do
begin
for j := low(a[i].test) to High(a[i].test) do
a[i].test[j] := i * 100 + j;
a[i].name.first := 'Test' + IntToStr(i);
end;
fs := TFileStream.Create('C:\temp\test.bin',fmCreate);
try
fs.Write(a,sizeOf(a));
finally
fs.Free;
end;
fs := TFileStream.Create('C:\temp\test.bin',fmopenRead);
try
fs.Read(b,sizeOf(b));
finally
fs.Free;
end;
for i := low(b) to High(b) do
begin
memo1.Lines.Add(b[i].name.first);
for j := low(b[i].test) to High(b[i].test) do
Memo1.Lines.Add(IntToStr(b[i].test[j]));
end;
end;
BTW:
Usual naming would be
Tnametype = record
first : string[10];
mi : string[3];
last : string[30];
end;
Tstype = record
id : integer;
name : nametype;
prog : array[1..10] of integer;
test : array[1..3] of integer;
progave, quizave : real;
average : double;
grade : char;
end;
Tsarraytype = array[1..100] of stype;
Upvotes: 6