可以用access函数来判断。
int access(const char *pathname, int mode);
下面是对参数mode的说明。一般来说,判断文件或文件夹是否存在,取 mode=F_OK 就可以了。
(或者说是读写权限)
access函数返回0表示成功,否则失败。
示例:
test.cpp
- #include <unistd.h> #include <iostream>
- using namespace std;
- int main(int argc, char* argv[]) {
- if(access(argv[1], F_OK) != 0) {
- cout << argv[1] << " does not exist!" << endl; }
- return 0;
- }
编译:
g++ test.cpp -o test
运行:
./test /some/folder
结果:
/some/folder does not exist!
此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/65323.html