bool IsFile(const std::string &filePath)
{
// 判断一个路径是文件
struct stat st;
if (0 == stat(filePath.c_str(), &st))
{
if (st.st_mode & S_IFDIR)
{
return false; // 目录
}
else if (st.st_mode & S_IFREG)
{
return true; // 文件
}
}
return false;
}
bool FastReadFile(const std::string &filePath, std::string &fileData, bool lock)
{
// buf的大小推荐4k或者1k都行, 一次性全读虽然快, 但是浪费内存
static constexpr long bufSize = 4096;
// 判断给定的路径是一个文件
if (!IsFile(filePath))
{
return false;
}
// 采用C方式读取文件信息, 比CPP的readline方式快N倍.
FILE *pFile;
if ((pFile = fopen(filePath.c_str(), "r")) == NULL)
{
return false;
}