_access函數
- int _access(
- const char *path,
- int mode
- );
- int _waccess(
- const wchar_t *path,
- int mode
- );
參數
path
文件或目錄路徑。
mode
讀/寫特性。
返回值
如果文件包含特定模式,每個函數返回 0。 函數返回 - 1,則名稱文件不存在或不具有特定模式;在這種情況下,如下表errno 所示設置。
EACCES
訪問被拒絕:文件權限的設置不允許指定的訪問權限。
ENOENT
未找到文件名或路徑。
EINVAL
無效參數。
備注
當使用文件,_access 函數確定指定的文件或目錄是否存在并且具有指定值的特性 mode。 在使用與目錄,_access 確定指定的目錄是否存在;只有在 Windows 2000 和更高版本的操作系統上,所有目錄讀取和寫入權限。
mode 值 | 檢查文件。 |
---|---|
00 | 僅存在 |
02 | Write-only |
04 | 只讀 |
06 | 讀取和寫入 |
最后給大家簡單匯總下VC++用于判斷文件文件夾是否存在的方法
推薦實例例:
if(::GetFileAttributes(m_filename)==-1){//文件不存在}else{//文件存在}
1. 使用_access函數,函數原型為 int _access( const char *path, int mode );
2. 使用CreateFile函數,函數原型為: HANDLE CreateFile( LPCTSTR lpFileName, // pointer to name of the file DWORD dwDesiredAccess, // access (read-write) mode DWORD dwShareMode, // share mode LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes DWORD dwCreationDisposition, // how to create DWORD dwFlagsAndAttributes, // file attributes HANDLE hTemplateFile // handle to file with attributes to // copy );
3. 使用FindFirstFile函數,函數原型為: HANDLE FindFirstFile( LPCTSTR lpFileName, // pointer to name of file to search for LPWIN32_FIND_DATA lpFindFileData // pointer to returned information );
4. 使用GetFileAttributes函數,函數原型如下: DWORD GetFileAttributes( LPCTSTR lpFileName // pointer to the name of a file or directory );
5. 使用Shell Lightweight Utility APIs函數 PathFileExists()專門判斷文件和目錄時否存在的函數文件名可讀性比較強還可以判斷目錄是否存在 Header: Declared in Shlwapi.h Import Library: Shlwapi.lib 以上的各種方法供參考,函數具體用法需參見MSDN
新聞熱點
疑難解答