您的位置:首页 > 运维架构 > Linux

linux下用access()判断文件是否存在

2013-12-31 20:26 453 查看
代码:

#include <unistd.h>
#include <stdio.h>

/**
* A example to check the file existence.
*
*/
int isFileExist(const char* filename) {
return access(filename, F_OK) == 0;
}

/**
* Usage: a.out filename
*
*/
int main(int argc, const char* argv[]) {
if (argc != 2) {
printf("Usage: a.out filename\n");
return 1;
}

if (isFileExist(argv[1])) {
printf("%s exists.\n", argv[1]);
} else {
printf("%s does not exist.\n", argv[1]);
}

return 0;
}


示例:

~/examples/cpp/access % ./a.out
Usage: a.out filename
~/examples/cpp/access % ./a.out a.out
a.out exists.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: