非原創:轉自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);
[編輯本段]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.
程序例:
- #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);
- }