【access系统调用】
 
功能描述:
检查调用进程是否可以对指定的文件执行某种操作。
 
用法:

#include<unistd.h>
 #include<fcntl.h>

 int access(const char *pathname, int mode);


 
参数:
pathname:需要测试的文件路径名。  

mode:需要测试的操作模式,可能值是一个或多个R_OK(可读?), W_OK(可写?), X_OK(可执行?) 或 F_OK(文件存在?)组合体。

if(access(g_LocalPath, R_OK | W_OK | F_OK) != -1)

{printf(“文件存在!”);}



返回说明:

成功执行时,返回0。失败返回-1,errno被设为以下的某个值

EINVAL: 模式值无效  

EACCES: 文件或路径名中包含的目录不可访问

ELOOP: 解释路径名过程中存在太多的符号连接

ENAMETOOLONG:路径名太长

ENOENT:  路径名中的目录不存在或是无效的符号连接

ENOTDIR: 路径名中当作目录的组件并非目录

EROFS: 文件系统只读

EFAULT: 路径名指向可访问的空间外

EIO:  输入输出错误

ENOMEM: 不能获取足够的内核内存


ETXTBSY:对程序写入出错




其他:


int   access(const   char   *filename,   int   amode); 
amode参数为0时表示检查文件的存在性,如果文件存在返回0,不存在返回-1。 

access会检查是否可以读/写某一已存在的文件。参数mode有几种情况组合, R_OK,W_OK,X_OK 和F_OK。R_OK,W_OK与X_OK用来检查文件是否具有读取、写入和执行的权限。F_OK则是用来判断该文件是否存在。由于access只作权限的核查,并不理会文件形态或文件内容,因此,如果一目录表示为“可写入”,表示可以在该目录中建立新文件等操作,而非意味此目录可以被当做文件处理。例如,你会发现DOS的文件都具有“可执行”权限,但用execve执行时则会失败。
这个函数还可以检查其它文件属性: 
06     检查读写权限 
04     检查读权限 
02     检查写权限 
01     检查执行权限 
00     检查文件的存在性
而这个就算这个文件没有读权限,也可以判断这个文件存在于否
存在返回0,不存在返回-1

C函数
  函数名: access 
  功 能: 确定文件的访问权限 
  用 法: int access(const char *filename, int amode);
[编辑本段]access
  Synopsis
  #include <io.h>
  int _access(const char *path,int mode) ;
  Description
  The access function, when used with files, determines whether the specified file exists and can be accessed as specified by the value of mode. When used with directories, _access determines only whether the specified directory exists; since under Windows all directories have read and write access.
  The mode argument can be one of :
  00 Existence only
  02 Write permission
  04 Read permission
  06 Read and write permission 
  Returns
  Zero if the file has the given mode, -1 if an error occurs.
  Portability :
  Windows. Under Unix a similar function exists too.
  Note that lcc-win32 accepts both _access (Microsoft convention) and access.
  程序例: 
  

1. #include <stdio.h> 
2. <io.h> 
3. (char *filename); 
4. (void) 
5. { 
6. ("Does NOTEXIST.FIL exist: %s\n", 
7. ("NOTEXISTS.FIL") ? "YES" : "NO"); 
8. ; 
9. } 
10. (char *filename) 
11. { 
12. (access(filename, 0) == 0); 
13. }




使用系统调用stat();
大概使用方法:
文件状态存储结构:struct stat file_info;
获取文件状态:stat(filename, &file_info);
文件类型和权限:mode_t mode = file_info.st_mode;
mode_t其实就是一个整型,例如0777,第一个数即为文件类型
使用系统相关宏来判断更方便
if ( S_ISDIR(mode) ) /* 目录 */
if ( S_ISREG(mode) ) /* 文件 */
if ( S_ISLNK(mode) ) /* 链接 */
还有包括判多是设备文件的等等,具体请自行查看sys/stat.h