Maxbad`Blog

一些小功能函数

2021-05-25 · 1 min read

文件是否存在

bool file_exists(const std::string& name)
{
    struct stat info;
	return (stat(name.c_str(), &info) == 0);
}

目录是否存在

bool dir_exists(const std::string& path)
{
	struct stat info;
	if(stat(path.c_str(), &info) != 0)
		return false; // cannot access
	if(info.st_mode & S_IFDIR)
		return true;
	return false;
}