Board index » delphi » File existance test

File existance test

Is there a way to test for the existance of files under ANSI/ISO pascal,
working under UNIX?  I can't think of anything standard that would do it,
nor anything that would be within the range of extensions found on
typical UNIX pasal compilers.  Any ideas?

--

[Jason Schlauch] | [sl...@glue.umd.edu] | [sl...@wam.umd.edu]
[http://www.wam.umd.edu/~slush] | [Random thought:]
[Two of the most famous products of Berkeley are LSD and Unix.]
[I don't that is a coincidence.]

 

Re:File existance test


Quote
>Is there a way to test for the existance of files under ANSI/ISO pascal,
>working under UNIX?  I can't think of anything standard that would do it,
>nor anything that would be within the range of extensions found on
>typical UNIX pasal compilers.  Any ideas?

     AFAIK, there is no such thing in the ANSI standard.  The closest one
might come is to try resetting the file and seeing if you are immediately at
EOF.

     Most implementations seem to have their own way of handling the reset()
procedure.  The one I use returns an optional filelength parameter that I can
test to see if it is non-zero (which implies the file exists).  Borland
probably uses some other mechanism ...

Bob Schor
Pascal Enthusiast

Re:File existance test


In <4cqfck$...@cville-srv.wam.umd.edu>, sl...@exp1.wam.umd.edu (A WiZaRD WiTH PLiaRS) writes:

Quote
>Is there a way to test for the existance of files under ANSI/ISO pascal,
>working under UNIX?  I can't think of anything standard that would do it,
>nor anything that would be within the range of extensions found on
>typical UNIX pasal compilers.  Any ideas?

Try to find out wether there is any way to trap errors. If there is a way, you
can just open the wanted file and trap any error if does noet exist.

Greetings,
Richard van der wal
_________________
rich...@sun3.iaf.nl

Re:File existance test


A WiZaRD WiTH PLiaRS wrote:

Quote

> Is there a way to test for the existance of files under ANSI/ISO pascal,
> working under UNIX?  I can't think of anything standard that would do it,
> nor anything that would be within the range of extensions found on
> typical UNIX pasal compilers.  Any ideas?

> --

Although theres no such thing as ANSI pascal.. heres the basic idea

1) use the reset fx with error checking off on the file in question
2) turn error checking on and check io result
3) if ioresult <> 0 then files doesnt exist.
4) else file exists

Hope this answers yr question.. [ i have _NO_ experience under unix so it probably wont :(]

Re:File existance test


How to find out if a file exists, given its filename.

How Borland Pascal <5.0-or-so did it:

Assign(InFile,FileName);
{$I-}  {switch off I/O error halting; all future I/O errors simply set the
        function result of the builtin "function IOResult: Integer" to a
        non-zero value, and don't crash.}
Reset(InFile);  {this _would_  crash, if not for the $I-, if the file didn't
                 exist; instead, it sets IOResult}
{$I+}  {switch checking back on}
if IOResult <> 0 then
  WriteLn('File does not exist!')
else
begin
  Close(InFile);  {don't forget this bit!}
  WriteLn('File exists!');
end;

How Borland 5.0+ does it:

if FileSearch(FileName,'') = '' then  {can't find matching name in directory}
  WriteLn('File does not exist!')
else
  WriteLn('File exists!');

How you can do it if you have a function like "FindFirst":

FindFirst(FileName, {other parameters});
if {whatever the result of that find was is erroneous, or the found name is
    blank} then
  WriteLn('File does not exist!')
else
  WriteLn('File exists!');

If you have FindFirst (a routine to find a file matching a filemask (eg
*.TXT) in the current directory, returning the name) or a similar directory
scanning routing, that'd be the way to go.  Alternatively, if your version
of Reset doesn't crash, test EoF immediately -- tho that would also be true
if the file exists but is empty.

Good luck!  I'd love to hear what you come up with!

: Eric :
--
Let's just say that if complete and utter chaos was light- |    Paul Sleigh
ning, he'd be the sort to stand on a hilltop in a thunder- | Eric the Fruitbat
storm wearing wet copper armour and shouting 'All gods are | fruitbat@canberra
bastards'.   [T Pratchett: Rincewind discussing Twoflower] |   .DIALix.oz.au

Other Threads