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.
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;