C语言中access函数

来源:互联网 发布:免费level2行情软件 编辑:程序博客网 时间:2024/05/20 03:44

C语言中access函数  来自于百度

  头文件:io.h
  功 能: 确定文件或文件夹的访问权限。即,检查某个文件的存取方式,比如说是只读方式、只写方式等。如果指定的存取方式有效,则函数返回0,否则函数返回-1。
  用 法: int access(const char *filenpath, int mode); 或者int _access( const char *path, int mode );
  参数说明:
  filenpath
  文件或文件夹的路径,当前目录直接使用文件或文件夹名
  备注:当该参数为文件的时候,access函数能使用mode参数所有的值,当该参数为文件夹的时候,access函数值能判断文件夹是否存在。在WIN NT 中,所有的文件夹都有读和写权限
  mode
  要判断的模式
  在头文件unistd.h中的预定义如下:
  #define R_OK 4 /* Test for read permission. */
  #define W_OK 2 /* Test for write permission. */
  #define X_OK 1 /* Test for execute permission. */
  #define F_OK 0 /* Test for existence. */
  具体含义如下:
  R_OK 只判断是否有读权限
  W_OK 只判断是否有写权限
  X_OK 判断是否有读并且有写权限
  F_OK 只判断是否存在
  access函数程序范例(C语言中)
  #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);
  }