delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/23/03:34:59

From: pjfarley AT dorsai DOT org (Peter J. Farley III)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: file_exists()
Date: Fri, 22 Aug 1997 23:48:33 GMT
Organization: None
Lines: 26
Message-ID: <33fe2408.3775959@snews.zippo.com>
References: <33fdf19a DOT 5293195 AT news DOT telepac DOT pt>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Marco Monteiro wrote:

>Hello,
>
>	how can i inspect if a file exists?
>	I'm using the function following function; is there a better one?
>
Marco,

Perusing the DJGPP libc sources, it seems to me that access(filename,
R_OK) will give you the results that you want, without the overhead of
an fopen and an fclose.  Just call:

access("filename.ext", R_OK)

Zero return value means the file is available for reading, which
implies that it actually exists.  So this code should work:

if (access("filename.ext", R_OK)) {
	fprint("FILE %s does not exist\n", "filename.ext");
	exit(1);
}
/* From here on we know the file exists... */

----------------------------------------------------
Peter J. Farley III (pjfarley AT dorsai DOT org)

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019