Header File
io.h
Category
Input/output Routines
Prototype
int access(const char *filename, int amode);
int _waccess(const wchar_t *filename, int amode);
Description
Determines accessibility of a file.
access checks the file named by filename to determine if it exists, and whether it can be read, written to, or executed.
The list of amode values is as follows:
06 |
Check for read and write permission |
04 |
Check for read permission |
02 |
Check for write permission |
01 |
Execute (ignored) |
00 |
Check for existence of file |
All existing files have read access (amode equals 04), so 00 and 04 give the same result. Similarly, amode values of 06 and 02 are equivalent because under Win32 write access implies read access.
If filename refers to a directory, access simply determines whether the directory exists.
Return Value
If the requested access is allowed, access returns 0; otherwise, it returns a value of -1, and the global variable errno is set to one of the following values:
ENOENT |
Path or file name not found |
EACCES |
Permission denied |
Example
#include <stdio.h> #include <io.h> int file_exists(char *filename); int main(void) { printf("Does NOTEXIST.FIL exist: %s\n", file_exists("NOTEXISTS.FIL") ? "YES" : "NO"); return 0; } int file_exists(char *filename) { return (access(filename, 0) == 0); }
Portability
|
POSIX |
Win32 |
ANSI C |
ANSI C++ |
access |
+ |
+ |
|
|
_waccess |
|
NT only |
|
|
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|