C++:优雅的读取文件路径、名称、后缀、父文件夹名称

本文记录了使用C++语言,读取文件的路径、文件名称、文件后缀、文件父文件夹名称的代码,分为指定后缀和不指定后缀两种。文中包括函数代码框架,可以根据需求修改,代码鲁棒性强,可扩展性强。

可以得到的结果如下所示:
文件全路径:“D:\RemoteSening\test\NDVI.jpg”
文件名称:“NDVI.jpg”
文件路径:“D:\RemoteSening\test”
文件后缀:“jpg”
文件父文件夹名称:“test”
文件名称:“NDVI”
文件路径下文件数量: n



一、代码框架

1.1 不指定后缀框架
//获得所有文件的父文件夹名称
vector<std::string> GetFiles(const std::string& inPath)
{
    
    
	int fileNum = 0;
	std::vector<std::string> pathVec;
	std::queue<std::string> q;
	q.push(inPath);
	while (!q.empty())
	{
    
    
		std::string item = q.front(); q.pop();
		//
		std::string path = item + "\\*";
		struct _finddata_t fileinfo;
		auto handle = _findfirst(path.c_str(), &fileinfo);
		if (handle == -1) continue;

		while (!_findnext(handle, &fileinfo))
		{
    
    
			if (fileinfo.attrib & _A_SUBDIR)
			{
    
    
				if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)continue;
				q.push(item + "\\" + fileinfo.name);
			}
			else
			{
    
    
			    //***********************需求开始****************************//
			    //所有的修改都在这个部分,根据你的需求修改
				//文件全路径(路径加名字)
				string strfilePath = item + "\\" + fileinfo.name;
                //
				//将路径保存在数组中
				pathVec.push_back(strfilePath);
				//***********************需求结束****************************//
			}
		}
		_findclose(handle);
	}
	return pathVec;
}
1.2 指定后缀框架
//获得所有文件的路径
vector<std::string> GetFilesWithSuffix(const std::string& inPath)
{
    
    
	int fileNum = 0;
	std::vector<std::string> pathVec;
	std::queue<std::string> q;
	q.push(inPath);
	while (!q.empty())
	{
    
    
		std::string item = q.front(); q.pop();
		//
		std::string path = item + "\\*";
		struct _finddata_t fileinfo;
		auto handle = _findfirst(path.c_str(), &fileinfo);
		if (handle == -1) continue;

		while (!_findnext(handle, &fileinfo))
		{
    
    
			if (fileinfo.attrib & _A_SUBDIR)
			{
    
    
				if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)continue;
				q.push(item + "\\" + fileinfo.name);
			}
			else
			{
    
    
			    //所有的修改都在这个部分,根据你的需求修改
				//文件全路径(路径加名字)
				string strfilePath = item + "\\" + fileinfo.name;
				int nLastSlashIndex = strfilePath.find_last_of('.');
				//读取文件后缀
				std::string strSuffix = strfilePath.substr(nLastSlashIndex+1, strfilePath.length());
				//判断是否为指定后缀
				if (strSuffix == "tif" || strSuffix == "jpg")
				{
    
    
				    //***********************需求开始****************************//
					//将结果保存在数组中
					pathVec.push_back(strfilePath);
					//***********************需求结束****************************//
				}
			}
		}
		_findclose(handle);
	}
	return pathVec;
}

二、读取文件名

2.1 不指定后缀
//获得所有文件文件名称
vector<std::string> GetFilesName(const std::string& inPath)
{
    
    
	int fileNum = 0;
	std::vector<std::string> pathVec;
	std::queue<std::string> q;
	q.push(inPath);
	while (!q.empty())
	{
    
    
		std::string item = q.front(); q.pop();
		//
		std::string path = item + "\\*";
		struct _finddata_t fileinfo;
		auto handle = _findfirst(path.c_str(), &fileinfo);
		if (handle == -1) continue;

		while (!_findnext(handle, &fileinfo))
		{
    
    
			if (fileinfo.attrib & _A_SUBDIR)
			{
    
    
				if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)continue;
				q.push(item + "\\" + fileinfo.name);
			}
			else
			{
    
    
			    //***********************需求开始****************************//
			    //所有的修改都在这个部分,根据你的需求修改
				//文件全路径(路径加名字)
				string strfilePath = item + "\\" + fileinfo.name;
				//
				int nLastSlashIndex = strfilePath.find_last_of('\\');
				//
				//读取文件名
                std::string strFileName = strfilePath.substr(nLastSlashIndex + 1, strfilePath.length());
                //
				//将文件名保存在数组中
				pathVec.push_back(strFileName);
				//***********************需求结束****************************//
			}
		}
		_findclose(handle);
	}
	return pathVec;
}
2.2 指定后缀
//获得所有文件的父文件夹名称
vector<std::string> GetFilesNameWithSuffix(const std::string& inPath)
{
    
    
	int fileNum = 0;
	std::vector<std::string> pathVec;
	std::queue<std::string> q;
	q.push(inPath);
	while (!q.empty())
	{
    
    
		std::string item = q.front(); q.pop();
		//
		std::string path = item + "\\*";
		struct _finddata_t fileinfo;
		auto handle = _findfirst(path.c_str(), &fileinfo);
		if (handle == -1) continue;

		while (!_findnext(handle, &fileinfo))
		{
    
    
			if (fileinfo.attrib & _A_SUBDIR)
			{
    
    
				if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)continue;
				q.push(item + "\\" + fileinfo.name);
			}
			else
			{
    
    
			    
			    //所有的修改都在这个部分,根据你的需求修改
				//文件全路径(路径加名字)
				string strfilePath = item + "\\" + fileinfo.name;
				//
				int nLastSlashIndex = strfilePath.find_last_of('.');
				//读取文件后缀
				std::string strSuffix = strfilePath.substr(nLastSlashIndex+1, strfilePath.length());
				//判断是否为指定后缀
				if (strSuffix == "tif" || strSuffix == "jpg")
				{
    
    
				    //***********************需求开始****************************//
					//
					int nLastSlashIndex = strfilePath.find_last_of('\\');
					//
					//读取文件名
	                std::string strFileName = strfilePath.substr(nLastSlashIndex + 1, strfilePath.length());
	                //
					//将文件名保存在数组中
					pathVec.push_back(strFileName);
					//***********************需求结束****************************//
                }			}
		}
		_findclose(handle);
	}
	return pathVec;
}

三、读取文件路径

3.1 不指定后缀
//获得所有文件的父文件夹名称
vector<std::string> GetFilesPath(const std::string& inPath)
{
    
    
	int fileNum = 0;
	std::vector<std::string> pathVec;
	std::queue<std::string> q;
	q.push(inPath);
	while (!q.empty())
	{
    
    
		std::string item = q.front(); q.pop();
		//
		std::string path = item + "\\*";
		struct _finddata_t fileinfo;
		auto handle = _findfirst(path.c_str(), &fileinfo);
		if (handle == -1) continue;

		while (!_findnext(handle, &fileinfo))
		{
    
    
			if (fileinfo.attrib & _A_SUBDIR)
			{
    
    
				if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)continue;
				q.push(item + "\\" + fileinfo.name);
			}
			else
			{
    
    
			    //***********************需求开始****************************//
			    //所有的修改都在这个部分,根据你的需求修改
				//文件全路径(路径加名字)
				string strfilePath = item + "\\" + fileinfo.name;
                //
				//将路径保存在数组中
				pathVec.push_back(strfilePath);
				//***********************需求结束****************************//
			}
		}
		_findclose(handle);
	}
	return pathVec;
}
3.2 指定后缀
//获得所有文件的路径
vector<std::string> GetFilesPathWithSuffix(const std::string& inPath)
{
    
    
	int fileNum = 0;
	std::vector<std::string> pathVec;
	std::queue<std::string> q;
	q.push(inPath);
	while (!q.empty())
	{
    
    
		std::string item = q.front(); q.pop();
		//
		std::string path = item + "\\*";
		struct _finddata_t fileinfo;
		auto handle = _findfirst(path.c_str(), &fileinfo);
		if (handle == -1) continue;

		while (!_findnext(handle, &fileinfo))
		{
    
    
			if (fileinfo.attrib & _A_SUBDIR)
			{
    
    
				if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)continue;
				q.push(item + "\\" + fileinfo.name);
			}
			else
			{
    
    
			    //所有的修改都在这个部分,根据你的需求修改
				//文件全路径(路径加名字)
				string strfilePath = item + "\\" + fileinfo.name;
				int nLastSlashIndex = strfilePath.find_last_of('.');
				//读取文件后缀
				std::string strSuffix = strfilePath.substr(nLastSlashIndex+1, strfilePath.length());
				//判断是否为指定后缀
				if (strSuffix == "tif" || strSuffix == "jpg")
				{
    
    
				    //***********************需求开始****************************//
					//将结果保存在数组中
					pathVec.push_back(strfilePath);
					//***********************需求结束****************************//
				}
			}
		}
		_findclose(handle);
	}
	return pathVec;
}

四、读取文件父文件夹名称

4.1 不指定后缀
//获得所有文件的父文件夹名称
//***********************需求开始****************************//
//文件全路径
string strfolderPathAndFileName = strfilePath;
//最后一个\\的索引
int nLastSlashIndex = strfolderPathAndFileName.find_last_of('\\');
//最后一个斜杠前的路径
std::string strPathFolder = strfolderPathAndFileName.substr(0, nLastSlashIndex);
//最后一个斜杠前的路径的最后一个文件名
std::string strPathLastPathFolder = strPathFolder.substr(strPathFolder.find_last_of('\\') + 1, strPathFolder.length());
//
pathVec.push_back(strPathLastPathFolder);
//***********************需求结束****************************//
4.2 指定后缀
//获得所有文件的父文件夹名称
//***********************需求开始****************************//
//文件全路径
string strfolderPathAndFileName = strfilePath;
//最后一个\\的索引
int nLastSlashIndex = strfolderPathAndFileName.find_last_of('\\');
//最后一个斜杠前的路径
std::string strPathFolder = strfolderPathAndFileName.substr(0, nLastSlashIndex);
//最后一个斜杠前的路径的最后一个文件名
std::string strPathLastPathFolder = strPathFolder.substr(strPathFolder.find_last_of('\\') + 1, strPathFolder.length());
//
pathVec.push_back(strPathLastPathFolder);
//***********************需求结束****************************//

五、读取文件数量

5.1 不指定后缀
//获得所有文件个数
int CFileStream::GetFilesNumber(const std::string& inPath)
{
    
    
	int fileNum = 0;
	std::vector<std::string> pathVec;
	std::queue<std::string> q;
	q.push(inPath);
	while (!q.empty())
	{
    
    
		std::string item = q.front(); q.pop();

		std::string path = item + "\\*";
		struct _finddata_t fileinfo;
		auto handle = _findfirst(path.c_str(), &fileinfo);
		if (handle == -1) continue;

		while (!_findnext(handle, &fileinfo))
		{
    
    
			if (fileinfo.attrib & _A_SUBDIR)
			{
    
    
				if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)continue;
				q.push(item + "\\" + fileinfo.name);
			}
			else
			{
    
    
				fileNum++;
				pathVec.push_back(item + "\\" + fileinfo.name);
			}
		}
		_findclose(handle);
	}
	return fileNum;
}
5.2 指定后缀
//获得所有文件个数
//文件全路径(路径加名字)
string strfilePath = item + "\\" + fileinfo.name;
int nLastSlashIndex = strfilePath.find_last_of('.');
//读取文件后缀
std::string strSuffix = strfilePath.substr(nLastSlashIndex+1, strfilePath.length());
//判断是否为指定后缀
if (strSuffix == "tif" || strSuffix == "jpg")
{
    
    
    //***********************需求开始****************************//
	//将结果保存在数组中
	fileNum++;
	//***********************需求结束****************************//
}

猜你喜欢

转载自blog.csdn.net/qq_35591253/article/details/129840935