Reputation: 1002
I used IOResult to verify the existence of a file, when it exists everything works fine but when the file doesn't exist the program closes abruptly.
I tried to use a try - except to catch the exception but the problem persists, perhaps I'm not using the exception properly? But not sure how should I use it.
function verifyExistence(var input:text):boolean;
var x:word; //IOResult
var r:boolean;
begin
try
{$I-}
reset(input);
{$I+}
x:=IOResult;
except on E: EInOutError do begin
r:=false;
exit;
end;
end;
if (x <> 0) then
r:=false //File does not exist
else
r:=true; //File exists
close(input);
verifyExistence:=r;
end;
procedure fopen(var path:string);
var exists:boolean;
begin
writeln('Specify file path. Example: C:\Users\Frank\Desktop\example.txt');
readln(path);
assign(input,path);
exists:=verifyExistence(input);
if exists then begin
//writeln('File exists');
end
else begin
writeln('File does not exist');
end;
end;
Upvotes: 1
Views: 96
Reputation: 1002
This is how I solved the problem:
I removed the verifyExistence function, just left fopen like this:
procedure fopen(var path:string);
begin
writeln('Specify file path. Example: C:\Users\Frank\Desktop\example.txt');
readln(path);
if not FileExists(path) then begin
writeln('File does not exist');
exit;
end;
assign(input,path);
end;
Thanks for the help
Upvotes: 1