判斷文件是否存在的另一種方法 _access 和 _waccess
阿新 • • 發佈:2018-04-07
alt char* clas printf star 版本 stdlib.h bar data
函數原型:
int _access( const char *path, int mode );
int _waccess( const wchar_t *path, int mode );
示例代碼:
[cpp] view plain copy
- #include <io.h>
- #include <stdio.h>
- #include <stdlib.h>
- int _tmain(int argc, _TCHAR* argv[])
- {
- //如果文件具有指定的訪問權限,則函數返回0
- //如果文件不存在或者不能訪問指定的權限,則返回-1
- //備註
- //當path為文件時,_access函數判斷文件是否存在,並判斷文件是否可以用mode值指定的模式進行訪問
- //當path為目錄時,_access只判斷指定的目錄是否存在,在WindowsNT和Windows2000中,所有目錄都有讀寫權限
- //mode值
- //00 只檢查文件是否存在
- //02 寫權限
- //04 讀權限
- //06 讀寫權限
- //_waccess是_access的寬字符版本
- if (_access("demo.txt", 0) != -1)
- {
- printf("the demo.txt exist\n");
- //判斷文件是否可寫,假定文件是只讀的
- if (_access("demo.txt", 2) == -1)
- {
- printf("the demo.txt does not have write permission\n");
- }
- else
- {
- printf("the demo.txt have write permission\n");
- }
- }
- else
- {
- printf("the demo.txt does not exist\n");
- }
- system("pause");
- return 0;
- }
https://blog.csdn.net/hellokandy/article/details/78471006
判斷文件是否存在的另一種方法 _access 和 _waccess