函數(shù)名: access
功 能: 確定文件的訪問(wèn)權(quán)限
用 法: int access(const char *filename, int amode);
以前一直沒(méi)用過(guò)這個(gè)函數(shù),今天調(diào)試程序發(fā)現(xiàn)了這個(gè)函數(shù),感覺(jué)挺好用,尤其是判斷一個(gè)文件或文件夾是否存在的時(shí)候,用不著再find了,文件的話還可以檢測(cè)讀寫(xiě)權(quán)限,文件夾的話則只能判斷是否存在,下面摘自MSDN:
int _access( const char *path, int mode );
Return Value
Each of these functions returns 0 if the file has the given mode. The function returns –1 if the named file does not exist or is not accessible in the given mode; in this case, errno is set as follows:
EACCES
Access denied: file’s permission setting does not allow specified access.
ENOENT
Filename or path not found.
Parameters
path
File or directory path
mode
Permission setting
Remarks
When used with files, the _access function 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; in Windows NT, all directories have read and write access.
mode Value Checks File For
00 Existence only
02 Write permission
04 Read permission
06 Read and write permission
Example
/* ACCESS.C: This example uses _access to check the
* file named "ACCESS.C" to see if it exists and if
* writing is allowed.
*/
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
/* Check for existence */
if( (_access( "ACCESS.C", 0 )) != -1 )
{
printf( "File ACCESS.C exists " );
/* Check for write permission */
if( (_access( "ACCESS.C", 2 )) != -1 )
printf( "File ACCESS.C has write permission " );
}
}
Output
File ACCESS.C existsFile ACCESS.C has write permission
3.在windows平臺(tái)下用API函數(shù)FindFirstFile(...):
(1)檢查文件是否存在:
(2)檢查某一目錄是否存在:
聯(lián)系客服