检测一个文件是否存在的方法
2015-12-24 10:29
731 查看
这个总结是参考了Stackoverflow的一遍文章http://stackoverflow.com/questions/12774207/fastest-way-to-check-if-a-file-exist-using-standard-c-c11-c,
方法如下
方法一:利用ifstream
bool exists(std::string file)
{
std::ifstreamfile(file);
boolstate = file.good();
file.close();
returnstate;
}
方法二:还是利用ifstream
bool exists(std::string file)
{
std::ifstreamfile(file);
if(file){
file.close();
returntrue;
}else{
file.close();
returnfalse;
}
}
方法三,利用Windows的API,PathFileExists
BOOL exists(LPCTSTR pszPath)
{
returnPathFileExists(pszPath);
}
方法四,利用Windows的API,GetFileAttributes
BOOL exists(LPCTSTR pszPath)
{
// Theattributes of the specified file or directory, returned in a DWORD,
//indicates success.0xFFFFFFFF indicates failure.
return(GetFileAttributes (pszPath) != 0xFFFFFFFF);
}
方法如下
方法一:利用ifstream
bool exists(std::string file)
{
std::ifstreamfile(file);
boolstate = file.good();
file.close();
returnstate;
}
方法二:还是利用ifstream
bool exists(std::string file)
{
std::ifstreamfile(file);
if(file){
file.close();
returntrue;
}else{
file.close();
returnfalse;
}
}
方法三,利用Windows的API,PathFileExists
BOOL exists(LPCTSTR pszPath)
{
returnPathFileExists(pszPath);
}
方法四,利用Windows的API,GetFileAttributes
BOOL exists(LPCTSTR pszPath)
{
// Theattributes of the specified file or directory, returned in a DWORD,
//indicates success.0xFFFFFFFF indicates failure.
return(GetFileAttributes (pszPath) != 0xFFFFFFFF);
}
相关文章推荐
- PHP全选 删除
- jQuery的选择器中的通配符[id^='code']或[name^='code']
- Spring给我的启发
- Oracle连接查询
- mysql优化技术
- Servlet和jsp对外访问路径的配置的…
- Spring中JdbcTemplate实现存储过程…
- 设计模式--工厂模式(三)
- 设计模式--工厂设计模式(二)
- 设计模式--工厂设计模式(一)
- 设计模式--单例设计模式
- 反射技术实现POI表格信息的注入
- Hibernate+SpringMVC整合:实战三,…
- Hibernate+SpringMVC整合:实战二,…
- Hibernate+SpringMVC整合实战一:搭…
- DAO层重构
- Action层重构
- service层重构的解决方案(三种解决…
- mybatis笔记
- Spring定时器的使用