Access of violation at address 00404094 with AssignFile();

我是研究僧i 提交于 2019-12-13 08:24:44

问题


I'm just getting back into Delphi after a few months of not touching it. Just want to refresh my mind a bit.

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;

I remember finding this error multiple times over the odd times I did Delphi, but I remember not using the CloseFile(); or Reset(); when getting the error.


回答1:


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.

There's no need for an array at all. You could use a single variable of type string to read each line.

It would all be easier like this though:

procedure TForm1.btn1Click(Sender: TObject);
begin  
  redOut.Lines.LoadFromFile('names.txt');
end;


来源:https://stackoverflow.com/questions/28229850/access-of-violation-at-address-00404094-with-assignfile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!