SUN Pascal: Determining if a file exists before opening
frist at ccu.umanitoba.ca
frist at ccu.umanitoba.ca
Thu Mar 22 03:42:00 AEST 1990
Last week, I posted the question
In SUN Pascal, how do you test to see if a file exists, before 'reset'ting
or 'rewrite'ing that file?
Many of the replies suggested calling the c access() procedure from
Pascal, while others sent c subroutines, or ideas for c subroutines, that
could be compiled separately and linked by the pc command. Below is what
I think is the simplest solution. Thanks a lot to all who replied to my
query.
const MAXLINE= 132;
type (* LINE simulates dynamic character strings in standard Pascal *)
CHARARRAY = packed array[1..MAXLINE] of char;
LINE = record
STR:CHARARRAY;
LEN:integer
end;
(* The c function access is found in sys/file.h. access must be declared in
the outermost scope of the Pascal program. See access (2) manual pages *)
function access(PATH:CHARARRAY; MODE:integer):integer; external c;
(* The following function may appear in any scope: *)
(* - - - - - - - - - - - - - - - - - - - - - - - - - - - *)
(* =true if file exists, =false if it doesn't *)
(* A comparable procedure is provided with ATT System V Pascal *)
(* A special thank you to Mark Foster, CIS Rearch Computing, Univ. of Penn.
and Glenn Gribble of Synaptics Inc. for showing me how to use access()
within Pascal programs. B.F. *)
function EXISTS(FILENAME:LINE):boolean;
const F_OK=0; (* checks if file exists *)
begin (* EXISTS *)
with FILENAME do begin
STR[LEN+1]:=chr(0); (* c strings terminate with null *)
if access(STR,F_OK)=-1 then EXISTS:=false
else EXISTS:=true
end (* with FILENAME *)
end; (* EXISTS *)
Brian Fristensky frist at ccu.umanitoba.ca
Assistant Professor
Dept. of Plant Science
University of Manitoba
Winnipeg, MB R3T 2N2 CANADA
Office phone: 204-474-6085
FAX: 204-275-5128
More information about the Comp.sys.sun
mailing list