【转载】C int access(const char *path, int amode); 判断文件是否存在

来源:互联网 发布:卧铺为什么要换票 知乎 编辑:程序博客网 时间:2024/06/06 18:05
    if( access(picPath, R_OK) != 0)    //0: success
        break;

from:
http://www.opengroup.org/onlinepubs/000095399/functions/access.html

NAME

access - determine accessibility of a file

SYNOPSIS

#include <unistd.h>

int access(const char *
path, int amode);

DESCRIPTION

The access() function shall check the file named by the pathname pointed to by the path argument for accessibility according to the bit pattern contained in amode, using the real user ID in place of the effective user ID and the real group ID in place of the effective group ID.

The value of amode is either the bitwise-inclusive OR of the access permissions to be checked (R_OK, W_OK, X_OK) or the existence test (F_OK).

If any access permissions are checked, each shall be checked individually, as described in the Base Definitions volume of IEEE Std 1003.1-2001, Chapter 3, Definitions. If the process has appropriate privileges, an implementation may indicate success for X_OK even if none of the execute file permission bits are set.

RETURN VALUE

If the requested access is permitted, access() succeeds and shall return 0; otherwise, -1 shall be returned and errno shall be set to indicate the error.

ERRORS

The access() function shall fail if:

[EACCES]
Permission bits of the file mode do not permit the requested access, or search permission is denied on a component of the path prefix.
[ELOOP]
A loop exists in symbolic links encountered during resolution of the path argument.
[ENAMETOOLONG]
The length of the path argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}.
[ENOENT]
A component of path does not name an existing file or path is an empty string.
[ENOTDIR]
A component of the path prefix is not a directory.
[EROFS]
Write access is requested for a file on a read-only file system.

The access() function may fail if:

[EINVAL]
The value of the amode argument is invalid.
[ELOOP]
More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the path argument.
[ENAMETOOLONG]
As a result of encountering a symbolic link in resolution of the path argument, the length of the substituted pathname string exceeded {PATH_MAX}.
[ETXTBSY]
Write access is requested for a pure procedure (shared text) file that is being executed.
0 0
原创粉丝点击