How to get file extension from string in C++

JeffV picture JeffV · Sep 9, 2008 · Viewed 175.5k times · Source

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

I need a cross platform solution.

Answer

brian newman picture brian newman · Sep 9, 2008

Is this too simple of a solution?

#include <iostream>
#include <string>

int main()
{
  std::string fn = "filename.conf";
  if(fn.substr(fn.find_last_of(".") + 1) == "conf") {
    std::cout << "Yes..." << std::endl;
  } else {
    std::cout << "No..." << std::endl;
  }
}