- #include <io.h>
- #include <iostream>
- using namespace std;
- int main()
- {
- if(_access(filename, 0) != -1)
- {
- cout<<filename<<"已经存在"<< endl;
- }
- else
- {
- cout<<filename <<"不存在"<< endl;
- }
- return 0;
- }
- /*
- int _access(const char *path, intmode);
- mode value
- 00 检测是否存在
- 02 写权限
- 04 读权限
- 06 读写权限
- */
方法1:
#include <iostream> #include <fstream> using namespace std; int main() { fstream _file; char *filename = "f:/untitled.bmp"; _file.open(filename, ios::in); if(!_file) { cout<<filename <<"没有被创建"<< endl; } else { cout<<filename<<"已经存在"<< endl; } return 0; }
方法2:
#include <io.h> #include <iostream> using namespace std; int main(void) { char *filename = "f:/untitled.bmp"; if(access(filename, 0) == 0) { cout<<filename<<"已经存在"<< endl; } else { cout<<filename <<"不存在"<< endl; }
return 0; } access(filename, 0) finename 文件名称 mode 模式,共5种模式: 0-检查文件是否存在 1-检查文件是否可运行 2-检查文件是否可写访问 4-检查文件是否可读访问 6-检查文件是否可读/写访问 */