How to get file extension from string in C++

前端 未结 25 2517
迷失自我
迷失自我 2020-11-30 22:35

Given a string \"filename.conf\", how to I verify the extension part?

I need a cross platform solution.

25条回答
  •  青春惊慌失措
    2020-11-30 23:11

    The best way is to not write any code that does it but call existing methods. In windows, the PathFindExtension method is probably the simplest.

    So why would you not write your own?

    Well, take the strrchr example, what happens when you use that method on the following string "c:\program files\AppleGate.Net\readme"? Is ".Net\readme" the extension? It is easy to write something that works for a few example cases, but can be much harder to write something that works for all cases.

提交回复
热议问题