首页 > 代码库 > 【转】C中的access函数

【转】C中的access函数

http://blog.chinaunix.net/uid-22785134-id-360282.html

int   access(const   char   *filename,   int   amode);
amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-1。
这个函数还可以检查其它文件属性:
06     检查读写权限
04     检查读权限
02     检查写权限
01     检查执行权限
00     检查文件的存在性
而这个就算这个文件没有读权限,也可以判断这个文件存在于否
存在返回0,不存在返回-1

C函数
  函数名: access
  功 能: 确定文件的访问权限
  用 法: int access(const char *filename, int amode);

#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); }