- 判断文件是否存在
- 判断文件是否为空
判断文件是否存在
与判断文件夹是否存在实现一致:
1 2 3 4 5 6
| #include <io.h> #include <direct.h> static bool isFileExist(const std::string& filePath) { return _access(filePath.c_str(), 0) != -1; }
|
判断文件是否为空
若文件不存在,则返回true
,若文件存在,读取第一个字符后,若到达文件尾,则说明文件为空,返回true
。
1 2 3 4 5 6 7 8 9 10
| #include <fstream>
static bool isFileEmpty(const std::string& filePath) { std::fstream fs(filePath); if (!fs) return true;
char c = fs.get(); return fs.eof(); }
|