Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file extension from string in C++

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

I need a cross platform solution.

like image 484
JeffV Avatar asked Sep 09 '08 13:09

JeffV


People also ask

How do I get the fileName from the file path?

To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null. Syntax: public static string GetFileName (string path);

What is .C file in C?

A file saved with c file extension is a source code file written in C programming language. The C file include all the implementation of application's functionality in the form of source code. The declaration of the source code is written in the header files that are saved with . h extension.


1 Answers

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;   } } 
like image 94
brian newman Avatar answered Oct 04 '22 20:10

brian newman