[C++][原创]std::string获取文件名后缀

Posted FL1623863129

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[C++][原创]std::string获取文件名后缀相关的知识,希望对你有一定的参考价值。

std::string GetFileExt(std::string &strFile, int isLower=0)
	
	if(isLower == 1)
	
		std::string strTemp = strFile;
		std::transform(strTemp.begin(), strTemp.end(), strTemp.begin(), ::tolower);
		std::string::size_type pos = strTemp.rfind('.');
		std::string strExt = strTemp.substr(pos == std::string::npos ? strTemp.length() : pos+1);
		return strExt;
	
	else
	
		std::string::size_type pos = strFile.rfind('.');
		std::string strExt = strFile.substr(pos == std::string::npos ? strFile.length() : pos+1);
		return strExt;
		

比如:输入yolov6s.onnx则执行函数返回onnx,注意返回没有点,如果只想获取文件路径+文件名无后缀,则

std::string Yolov6Manager::GetFileNameWithPath(std::string &strFile)
	
		std::string::size_type pos = strFile.rfind('.');
		std::string str = strFile.substr(0,pos);
		return str;
	

以上是关于[C++][原创]std::string获取文件名后缀的主要内容,如果未能解决你的问题,请参考以下文章