Name
access - Determines whether a file exists and is accessible.
Syntax
#include <io.h>
int access(pathname, mode)
char *pathname;
int mode;
Description
With files, the access function determines whether or not
the specified file exists and can be accessed in mode. The
possible mode values and their meanings in the access call
are as follows:
Value Meaning
00 Check for existence only
02 Check for write permission
04 Check for read permission
06 Check for read and write permission
Under DOS, all existing files have read access; thus modes
00 and 04 produce the same result. Similarly modes 02 and
06 are equivalent, since write access implies read access on
DOS.
With directories, access determines only whether the
specified directory exists; under DOS and OS/2, all
directories have read and write access.
Return Value
The access function returns the value 0 if the file has the
given mode. A return value of -1 indicates that the named
file does not exist or is not accessible in the given mode,
and errno is set to one of the following values:
Value Meaning
EACCES Access denied; the file permission
setting does not allow the specified
access.
ENOENT File or path name not found.
See Also
chmod(DOS), fstat(DOS), open(DOS), stat(DOS)
Example
#include <io.h> #include <fcntl.h> #include <stdio.h>
int fh;
main()
{
/* check for write permission:*/
if ((access("data", 2)) == -1 )
{
perror("Data file not writable");
exit(1);
}
else
{
fh = open("data",O_WRONLY);
printf("Data file writable and opened for
output\n");
}
}
This example uses access to check the file named data to see
if writing is allowed.
(printed 6/18/89)