我正在将文本文件读入记录数组。文本文件中的部分数据是程序等级和测试等级。我可以声明一个整数数组来折叠程序或测试值作为记录中的字段吗?如果是,那么如何访问字段中的各个程序值? 例如,我可以像这样声明记录吗?如果是这样,我将如何输入或访问来自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;
所以我猜我在哪里读到了这些数组。到目前为止,我读到了:
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;
我已阅读其他数据,但我完全不知道如何阅读编程和测试数组。
答案 0 :(得分:6)
你可以。举个例子......
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;
作为对您的评论的回复
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;
顺便说一句:
通常的命名是
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;