您的位置:首页 > 其它

如何获取一个文件后缀?

2014-09-23 19:04 134 查看
场景: 图片格式判断和字符大小转换

用到函数

strrchr() 从字符的最后一个位置开始读起

strchr() 从字符的第一个位置开始读起

find() 查找字符串

#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>

using namespace std;

std::string GetFilePosfix(const char* path)
{//获取文件后缀
	std::string path_pos=strrchr(path,'.');
	std::string path_format(path_pos,1);
	std::cout<<"fun:"<<path_format<<endl;
	transform(path_format.begin(),path_format.end(),path_format.begin(),::tolower);
	return path_format;
}

bool IsSupported(const std::string currnet_format,const std::string supported_format)
{//判断后缀是否是支持的格式
	std::string tmp_str(";");
	tmp_str.append(currnet_format).append(";");

	if(supported_format.find(tmp_str)!= supported_format.npos)//npos 不存在的位置,一般为 -1
	{
		return true;
	}
	return false;
}

int main(int argc,char *argv[])
{
	const char* path="C:/file/test.dd.pnG";
	
	cout<<GetFilePosfix(path)<<endl;
	
	const std::string image_format=";png;bmp;gif;png";

	cout<<IsSupported(GetFilePosfix(path),image_format)<<endl;

	system("pause");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐