问题描述
几个月后,我才回到Delphi。
I'm just getting back into Delphi after a few months of not touching it. Just want to refresh my mind a bit.
在 AssignFile();
。我正在做的只是通过文本文件将名称列表读入一个丰富的编辑中。
I keep getting an access violation at the part of AssignFile();
. What I'm doing is just reading a list of names into a rich edit via a text file.
procedure TForm1.btn1Click(Sender: TObject);
var
k : Integer;
MyArray : array[1..1000] of string;
begin
k := 1;
AssignFile(MyFile, 'names.txt');
Reset(MyFile);
while not Eof(MyFile) do // <-- Here is the error
begin
readln(MyFile, MyArray[k]);
redOut.Lines.Add(MyArray[k]);
Inc(k);
end;
CloseFile(MyFile);
end;
我记得在执行Delphi的奇数次中多次发现此错误,但我记得没有使用 CloseFile();
或 Reset();
出现错误时。
I remember finding this error multiple times over the odd times I did Delp but I remember not using the CloseFile();
or Reset();
when getting the error.
推荐答案
在给出该代码的情况下,很难看出错误的出处。一种可能是注销静态大小的数组的末尾。
It's a little hard to see where the error originates given that code. One possibility is that you write off the end of the statically sized array.
根本不需要数组。您可以使用 string
类型的单个变量来读取每一行。
There's no need for an array at all. You could use a single variable of type string
to read each line.
虽然这样会更容易:
procedure TForm1.btn1Click(Sender: TObject);
begin
redOut.Lines.LoadFromFile('names.txt');
end;
这篇关于使用AssignFile()访问地址00404094上的违规;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!