职坐标c语言,如何使用C语言获取一个文件后缀?

本文主要向大家介绍了如何使用C语言获取一个文件后缀,通过具体的代码向大家展示,希望对大家学习C语言有所帮助。

场景: 图片格式判断和字符大小转换

用到函数

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

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

find()  查找字符串[cpp] view plain copy

1. #include 

2. #include 

3. #include 

4. #include 

5. #include 

6.

7. using namespace std;

8.

9. std::string GetFilePosfix(const char* path)

10. {//获取文件后缀

11.     std::string path_pos=strrchr(path,'.');

12.     std::string path_format(path_pos,1);

13.     std::cout<

14.     transform(path_format.begin(),path_format.end(),path_format.begin(),::tolower);

15.     return path_format;

16. }

17.

18. bool IsSupported(const std::string currnet_format,const std::string supported_format)

19. {//判断后缀是否是支持的格式

20.     std::string tmp_str(";");

21.     tmp_str.append(currnet_format).append(";");

22.

23.     if(supported_format.find(tmp_str)!= supported_format.npos)//npos 不存在的位置,一般为 -1

24.     {

25.         return true;

26.     }

27.     return false;

28. }

29.

30. int main(int argc,char *argv[])

31. {

32.     const char* path="C:/file/test.dd.pnG";

33.

34.     cout<

35.

36.     const std::string image_format=";png;bmp;gif;png";

37.

38.     cout<

39.

40.     system("pause");

41. }

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值