1. 程式人生 > >判斷文件是否存在的另一種方法 _access 和 _waccess

判斷文件是否存在的另一種方法 _access 和 _waccess

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
  1. #include <io.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int _tmain(int argc, _TCHAR* argv[])
  5. {
  6. //如果文件具有指定的訪問權限,則函數返回0
  7. //如果文件不存在或者不能訪問指定的權限,則返回-1
  8. //備註
  9. //當path為文件時,_access函數判斷文件是否存在,並判斷文件是否可以用mode值指定的模式進行訪問
  10. //當path為目錄時,_access只判斷指定的目錄是否存在,在WindowsNT和Windows2000中,所有目錄都有讀寫權限
  11. //mode值
  12. //00 只檢查文件是否存在
  13. //02 寫權限
  14. //04 讀權限
  15. //06 讀寫權限
  16. //_waccess是_access的寬字符版本
  17. if (_access("demo.txt", 0) != -1)
  18. {
  19. printf("the demo.txt exist\n");
  20. //判斷文件是否可寫,假定文件是只讀的
  21. if (_access("demo.txt", 2) == -1)
  22. {
  23. printf("the demo.txt does not have write permission\n");
  24. }
  25. else
  26. {
  27. printf("the demo.txt have write permission\n");
  28. }
  29. }
  30. else
  31. {
  32. printf("the demo.txt does not exist\n");
  33. }
  34. system("pause");
  35. return 0;
  36. }

https://blog.csdn.net/hellokandy/article/details/78471006

判斷文件是否存在的另一種方法 _access 和 _waccess