I'm using old style Pascal I/O routines and expect that calls to I/O functions that fail should raise an EInOutError
. When I try this I do not see an exception raised and I have no clue why.
procedure TForm1.Button1Click(Sender: TObject);
//var i: integer;
begin
id:=(strtoint(Edit1.Text)-1)*4;
AssignFile(plik,'\klienci\'+linia_klient[id]+'.txt');
try
Reset(plik);
except
on EInOutError do Rewrite(plik);
end;
edit2.Text:=linia_klient[id+1];
edit3.Text:=linia_klient[id+2];
//ListBox1.Clear;
//ListBox1.Items.Add();
end;
Entire code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Label2: TLabel;
Label3: TLabel;
Edit2: TEdit;
Edit3: TEdit;
ListBox1: TListBox;
Label4: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
plik:TextFile;
linia_klient,linia_video:array[0..20] of string;
id:integer;
implementation
{$R *.dfm}
{$IOCHECKS ON}
procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
Edit1.Text:='Witaj, Podaj ID klienta';
Label1.Caption:='ID';
AssignFile(plik,'klienci.txt');
Reset(plik);
i:=0;
While Not Eof(plik) do
begin
Readln(plik,linia_klient[i]);
inc(i);
end;
CloseFile(plik);
AssignFile(plik,'video.txt');
Reset(plik);
i:=0;
While Not Eof(plik) do
begin
Readln(plik,linia_video[i]);
inc(i);
end;
CloseFile(plik);
end;
procedure TForm1.Button1Click(Sender: TObject);
//var i: integer;
begin
id:=(strtoint(Edit1.Text)-1)*4;
AssignFile(plik,'\klienci\'+linia_klient[id]+'.txt');
try
Reset(plik);
except
on EInOutError do Rewrite(plik);
end;
edit2.Text:=linia_klient[id+1];
edit3.Text:=linia_klient[id+2];
//ListBox1.Clear;
//ListBox1.Items.Add();
end;
end.
The exception EInOutError
will only be raised if I/O checking is enabled. To make sure it is enabled do the following:
This should give you a proper exception if the file doesn't exist.
Now if (for whatever reason) you cannot enable I/O checking:
With disabled I/O checking you won't get an EInOutError
if something goes wrong. Instead you have to check the value of IOResult after every I/O operation. It's like in old Pascal times: If IOResult <> 0
then an error happened.
This (slightly adapted) excerpt from the Delphi docs shows how to work with IOResult:
AssignFile(F, FileName);
{$I-}
Reset(F);
{$I+}
if IOResult = 0 then
begin
MessageDlg('File size in bytes: ' + IntToStr(FileSize(F)),
mtInformation, [mbOk], 0);
CloseFile(F);
end
else
MessageDlg('File access error', mtWarning, [mbOk], 0);
However, nowadays you should use TFileStream
to access/create files and don't use the old style Pascal routines anymore. An example how this could look:
filename := '\klienci\'+linia_klient[id]+'.txt';
if not FileExists(filename) then
// "Create a file with the given name. If a file with the given name exists, open the file in write mode."
fs := TFileStream.Create(filename, fmCreate) else
// "Open the file to modify the current contents rather than replace them."
fs := TFileStream.Create(filename, fmOpenReadWrite);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With