C语言检测指定文件是否存在的代码

内容之余,将做工程过程中比较常用的一些内容片段珍藏起来,下面资料是关于C语言检测指定文件是否存在的内容,希望能对小伙伴们有所用。

#include <stdbool.h>
#include <stdio.h>

{

if (!f) return false;

fclose(f);

return true;
}





更好的版本


#include <unistd.h>

return !access(filename, F_OK);
}





或者还可以更短


#include <unistd.h>

#define exists(filename) (!access(filename, F_OK))

 




C++版本


#include <fstream>
#include <string>
bool file_exists(const std::string& s) {
std::ifstream iff(s.c_str());
return iff.is_open();
}





posted @ 2019-01-23 17:04  玉米帮  阅读(1195)  评论(0编辑  收藏  举报