C语言库函数access的使用

来源:互联网 发布:mac怎么修改hosts文件 编辑:程序博客网 时间:2024/06/06 09:23

另外一种利用 c 语言的库的办法:

函数名: access
功  能: 确定文件的访问权限
用  法: int access(const char *filenames, 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);
}



access(filename, 0)0 表示判断文件是否存在

finename 文件名称                                      mode 模式,共5种模式:                                 

0-检查文件是否存在         

1-检查文件是否可运行        

2-检查文件是否可写访问    

4-检查文件是否可读访问     

6-检查文件是否可读/写访问

 

access 返回值是0的时候,表示存在,而返回-1的时候,表示失败。

    if(access(_Filename,0) == 0)
        printf("存在");
    else
        不存在。

原创粉丝点击