[comp.lang.pascal] Find File?

fer9483@tesla.njit.edu (02/11/91)

I have a question about files in TP 5.5

I have a program for a class that needs to save and load a file.

That all works fine, but I need to know how to check if the file exists
or not.  If the program is being run for the 1st time, and they try and
load, and the file isn't there, I get a run-tim error.  Is there a function
to check if a file exists?

-Frank Rachel

John G. Spragge <SPRAGGEJ@QUCDN.QueensU.CA> (02/12/91)

Regarding a question about how to check and see if a file exists:

There are two methods. The first is to try and open the file with
Pascal run-time I/O error halting turned off. This is set by the
$I switch; thus, if you include {$I-} in your file, an attempt
to open the file will not halt the program. To see whether the file
was opened successfully, use the IORESULT function.

The second method is to use the FINDFIRST command.

You can find all these functions and options documented in your
manual.

disclaimer: Queen's University supplies me with computer services, not
            my opinions.

John G. Spragge

reino@cs.eur.nl (Reino de Boer) (02/15/91)

In <91042.205829SPRAGGEJ@QUCDN.QueensU.CA> SPRAGGEJ@QUCDN.QueensU.CA (John G. Spragge) writes:

>Regarding a question about how to check and see if a file exists:

>There are two methods. The first is to try and open the file with
>Pascal run-time I/O error halting turned off. This is set by the
>$I switch; thus, if you include {$I-} in your file, an attempt
>to open the file will not halt the program. To see whether the file
>was opened successfully, use the IORESULT function.

Assuming you mean something like

function exists( filename : pathstr ) : boolean;
	var 	f : file;
	begin
		assign( f, filename );
		{$I-}
		reset( f );
		{$I+}
		if ioresult = 0 then begin
  			{ file exists }
			close( f );
			exists := true
		end
		else
  			{ file doesn't exist }
			exists := false
	end;

This method can fail for two reasons:
1.  	in old versions of Turbo Pascal
		exists( 'con' ) 
	gives an incorrect result, because it's explicitly of type text.
	But, more importantly:
2.	the file is not found if it's a read-only file on a network
	drive. This is because untyped files (and any other non-text files)
	are opened in update mode by reset( .. ).

>The second method is to use the FINDFIRST command.

And be sure to check whether it is a volume-label, directory, or file.

Hope this helps -- Reino
-- 
Reino R. A. de Boer     "We want to build the right product right, right?"
Erasmus University Rotterdam ( Informatica )
e-mail: reino@cs.eur.nl

ts@uwasa.fi (Timo Salmi) (02/16/91)

In article <1991Feb15.112843.13779@cs.eur.nl> reino@cs.eur.nl writes:
>In <91042.205829SPRAGGEJ@QUCDN.QueensU.CA> SPRAGGEJ@QUCDN.QueensU.CA (John G. Spragge) writes:
>
>>Regarding a question about how to check and see if a file exists:
:
>2.	the file is not found if it's a read-only file on a network
>	drive. This is because untyped files (and any other non-text files)
>	are opened in update mode by reset( .. ).
:

Have you checked what is the effect of FileMode on net files.  I
pose view this in the format of a quiestion, because I have not
dealt with net files, but on the other hand the value of the
FileMode variable counts on local files. 

...................................................................
Prof. Timo Salmi        
Moderating at garbo.uwasa.fi anonymous ftp archives 128.214.12.37
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun