How to get file extension from string in C++

C++StringFilenamesFile Extension

C++ Problem Overview


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

I need a cross platform solution.

C++ Solutions


Solution 1 - C++

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;
  }
}

Solution 2 - C++

The best way is to not write any code that does it but call existing methods. In windows, the [PathFindExtension][1] 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.

[1]: http://msdn.microsoft.com/en-us/library/bb773587(VS.85).aspx "PathFindExtension"

Solution 3 - C++

You have to make sure you take care of file names with more then one dot. example: c:\.directoryname\file.name.with.too.many.dots.ext would not be handled correctly by strchr or find.

My favorite would be the boost filesystem library that have an extension(path) function

Solution 4 - C++

Assuming you have access to STL:

std::string filename("filename.conf");
std::string::size_type idx;

idx = filename.rfind('.');

if(idx != std::string::npos)
{
    std::string extension = filename.substr(idx+1);
}
else
{
    // No extension found
}

Edit: This is a cross platform solution since you didn't mention the platform. If you're specifically on Windows, you'll want to leverage the Windows specific functions mentioned by others in the thread.

Solution 5 - C++

With C++17 and its std::filesystem::path::extension (the library is the successor to boost::filesystem) you would make your statement more expressive than using e.g. std::string.

#include <iostream>
#include <filesystem> // C++17
namespace fs = std::filesystem;

int main()
{
	fs::path filePath = "my/path/to/myFile.conf";
	if (filePath.extension() == ".conf") // Heed the dot.
    {
		std::cout << filePath.stem() << " is a valid type."; // Output: "myFile is a valid type."
    }
	else
    {
		std::cout << filePath.filename() << " is an invalid type."; // Output: e.g. "myFile.cfg is an invalid type"
    }
}

See also std::filesystem::path::stem, std::filesystem::path::filename.

Solution 6 - C++

Someone else mentioned boost but I just wanted to add the actual code to do this:

#include <boost/filesystem.hpp>
using std::string;
string texture         = foo->GetTextureFilename();
string file_extension  = boost::filesystem::extension(texture);
cout << "attempting load texture named " << texture
     << "    whose extensions seems to be " 
     << file_extension << endl;
// Use JPEG or PNG loader function, or report invalid extension

Solution 7 - C++

actually the STL can do this without much code, I advise you learn a bit about the STL because it lets you do some fancy things, anyways this is what I use.

std::string GetFileExtension(const std::string& FileName)
{
	if(FileName.find_last_of(".") != std::string::npos)
		return FileName.substr(FileName.find_last_of(".")+1);
	return "";
}

this solution will always return the extension even on strings like "this.a.b.c.d.e.s.mp3" if it cannot find the extension it will return "".

Solution 8 - C++

Actually, the easiest way is

char* ext;
ext = strrchr(filename,'.') 

One thing to remember: if '.' doesn't exist in filename, ext will be NULL.

Solution 9 - C++

I've stumbled onto this question today myself, even though I already had a working code I figured out that it wouldn't work in some cases.

While some people already suggested using some external libraries, I prefer to write my own code for learning purposes.

Some answers included the method I was using in the first place (looking for the last "."), but I remembered that on linux hidden files/folders start with ".". So if file file is hidden and has no extension, the whole file name would be taken for extension. To avoid that I wrote this piece of code:

bool getFileExtension(const char * dir_separator, const std::string & file, std::string & ext)
{
    std::size_t ext_pos = file.rfind(".");
    std::size_t dir_pos = file.rfind(dir_separator);

    if(ext_pos>dir_pos+1)
    {
        ext.append(file.begin()+ext_pos,file.end());
        return true;
    }

    return false;
}

I haven't tested this fully, but I think that it should work.

Solution 10 - C++

I'd go with boost::filesystem::extension (std::filesystem::path::extension with C++17) but if you cannot use Boost and you just have to verify the extension, a simple solution is:

bool ends_with(const std::string &filename, const std::string &ext)
{
  return ext.length() <= filename.length() &&
         std::equal(ext.rbegin(), ext.rend(), filename.rbegin());
}

if (ends_with(filename, ".conf"))
{ /* ... */ }

Solution 11 - C++

Using std::string's find/rfind solves THIS problem, but if you work a lot with paths then you should look at boost::filesystem::path since it will make your code much cleaner than fiddling with raw string indexes/iterators.

I suggest boost since it's a high quality, well tested, (open source and commercially) free and fully portable library.

Solution 12 - C++

_splitpath, _wsplitpath, _splitpath_s, _wsplitpath_w

This is Windows (Platform SDK) only

Solution 13 - C++

For char array-type strings you can use this:

#include <ctype.h>
#include <string.h>
    
int main()
{
	char filename[] = "apples.bmp";
    char extension[] = ".jpeg";

	if(compare_extension(filename, extension) == true)
    {
		// .....
	} else {
		// .....
    }

    return 0;
}

bool compare_extension(char *filename, char *extension)
{
	/* Sanity checks */

	if(filename == NULL || extension == NULL)
		return false;

	if(strlen(filename) == 0 || strlen(extension) == 0)
		return false;

	if(strchr(filename, '.') == NULL || strchr(extension, '.') == NULL)
		return false;

	/* Iterate backwards through respective strings and compare each char one at a time */

	for(int i = 0; i < strlen(filename); i++)
	{
		if(tolower(filename[strlen(filename) - i - 1]) == tolower(extension[strlen(extension) - i - 1]))
		{
			if(i == strlen(extension) - 1)
				return true;
		} else
			break;
	}

	return false;
}

Can handle file paths in addition to filenames. Works with both C and C++. And cross-platform.

Solution 14 - C++

A NET/CLI version using System::String

   System::String^ GetFileExtension(System::String^ FileName)
   {
       int Ext=FileName->LastIndexOf('.');
       if( Ext != -1 )
           return FileName->Substring(Ext+1);
       return "";
   }

Solution 15 - C++

If you use Qt library, you can give a try to QFileInfo's suffix()

Solution 16 - C++

Good answers but I see most of them has some problems: First of all I think a good answer should work for complete file names which have their path headings, also it should work for linux or windows or as mentioned it should be cross platform. For most of answers; file names with no extension but a path with a folder name including dot, the function will fail to return the correct extension: examples of some test cases could be as follow:

    const char filename1 = {"C:\\init.d\\doc"}; // => No extention
    const char filename2 = {"..\\doc"}; //relative path name => No extention
    const char filename3 = {""}; //emputy file name => No extention
    const char filename4 = {"testing"}; //only single name => No extention
    const char filename5 = {"tested/k.doc"}; // normal file name => doc
    const char filename6 = {".."}; // parent folder => No extention
    const char filename7 = {"/"}; // linux root => No extention
    const char filename8 = {"/bin/test.d.config/lx.wize.str"}; // ordinary path! => str

"brian newman" suggestion will fail for filename1 and filename4. and most of other answers which are based on reverse find will fail for filename1. I suggest including the following method in your source: which is function returning index of first character of extension or the length of given string if not found.

size_t find_ext_idx(const char* fileName)
{
    size_t len = strlen(fileName);
    size_t idx = len-1;
    for(size_t i = 0; *(fileName+i); i++) {
        if (*(fileName+i) == '.') {
            idx = i;
        } else if (*(fileName + i) == '/' || *(fileName + i) == '\\') {
            idx = len - 1;
        }
    }
    return idx+1;
}

you could use the above code in your c++ application like below:

std::string get_file_ext(const char* fileName)
{
    return std::string(fileName).substr(find_ext_idx(fileName));
}

The last point in some cases the a folder is given to file name as argument and includes a dot in the folder name the function will return folder's dot trailing so better first to user check that the given name is a filename and not folder name.

Solution 17 - C++

This is a solution I came up with. Then, I noticed that it is similar to what @serengeor posted.

It works with std::string and find_last_of, but the basic idea will also work if modified to use char arrays and strrchr. It handles hidden files, and extra dots representing the current directory. It is platform independent.

string PathGetExtension( string const & path )
{
  string ext;

  // Find the last dot, if any.
  size_t dotIdx = path.find_last_of( "." );
  if ( dotIdx != string::npos )
  {
    // Find the last directory separator, if any.
    size_t dirSepIdx = path.find_last_of( "/\\" );

    // If the dot is at the beginning of the file name, do not treat it as a file extension.
    // e.g., a hidden file:  ".alpha".
    // This test also incidentally avoids a dot that is really a current directory indicator.
    // e.g.:  "alpha/./bravo"
    if ( dotIdx > dirSepIdx + 1 )
    {
      ext = path.substr( dotIdx );
    }
  }

  return ext;
}

Unit test:

int TestPathGetExtension( void )
{
  int errCount = 0;

  string tests[][2] = 
  {
    { "/alpha/bravo.txt", ".txt" },
    { "/alpha/.bravo", "" },
    { ".alpha", "" },
    { "./alpha.txt", ".txt" },
    { "alpha/./bravo", "" },
    { "alpha/./bravo.txt", ".txt" },
    { "./alpha", "" },
    { "c:\\alpha\\bravo.net\\charlie.txt", ".txt" },
  };

  int n = sizeof( tests ) / sizeof( tests[0] );

  for ( int i = 0; i < n; ++i )
  {
    string ext = PathGetExtension( tests[i][0] );
    if ( ext != tests[i][1] )
    {
      ++errCount;
    }
  }

  return errCount;
}

Solution 18 - C++

You can use strrchr() to find last occurence of .(dot) and get .(dot) based extensions files. Check the below code for example.

#include<stdio.h>

void GetFileExtension(const char* file_name) {

    int ext = '.';
    const char* extension = NULL;
    extension = strrchr(file_name, ext);

    if(extension == NULL){
        printf("Invalid extension encountered\n");
        return;
    }

    printf("File extension is %s\n", extension);
}

int main()
{
    const char* file_name = "c:\\.directoryname\\file.name.with.too.many.dots.ext";
    GetFileExtension(file_name);
    return 0;
}

Solution 19 - C++

Here's a function that takes a path/filename as a string and returns the extension as a string. It is all standard c++, and should work cross-platform for most platforms.

Unlike several other answers here, it handles the odd cases that windows' PathFindExtension handles, based on PathFindExtensions's documentation.

wstring get_file_extension( wstring filename )
{
	size_t last_dot_offset = filename.rfind(L'.');
	// This assumes your directory separators are either \ or /
	size_t last_dirsep_offset = max( filename.rfind(L'\\'), filename.rfind(L'/') );
	
	// no dot = no extension
	if( last_dot_offset == wstring::npos )
		return L"";
	
	// directory separator after last dot = extension of directory, not file.
	// for example, given C:\temp.old\file_that_has_no_extension we should return "" not "old"
	if( (last_dirsep_offset != wstring::npos) && (last_dirsep_offset > last_dot_offset) )
		return L"";
	
	return filename.substr( last_dot_offset + 1 );
}

Solution 20 - C++

I use these two functions to get the extension and filename without extension:

std::string fileExtension(std::string file){
    
    std::size_t found = file.find_last_of(".");
    return file.substr(found+1);
    
}

std::string fileNameWithoutExtension(std::string file){
    
    std::size_t found = file.find_last_of(".");
    return file.substr(0,found);    
}

And these regex approaches for certain extra requirements:

std::string fileExtension(std::string file){
    
    std::regex re(".*[^\\.]+\\.([^\\.]+$)");
    std::smatch result;
    if(std::regex_match(file,result,re))return result[1];
    else return "";
    
}

std::string fileNameWithoutExtension(std::string file){
  
    std::regex re("(.*[^\\.]+)\\.[^\\.]+$");
    std::smatch result;
    if(std::regex_match(file,result,re))return result[1];
    else return file;
    
}

Extra requirements that are met by the regex method:

  1. If filename is like .config or something like this, extension will be an empty string and filename without extension will be .config.
  2. If filename doesn't have any extension, extention will be an empty string, filename without extension will be the filename unchanged.

EDIT:

The extra requirements can also be met by the following:

std::string fileExtension(const std::string& file){
    std::string::size_type pos=file.find_last_of('.');
    if(pos!=std::string::npos&&pos!=0)return file.substr(pos+1);
    else return "";
}


std::string fileNameWithoutExtension(const std::string& file){
    std::string::size_type pos=file.find_last_of('.');
    if(pos!=std::string::npos&&pos!=0)return file.substr(0,pos);
    else return file;
}

Note:

Pass only the filenames (not path) in the above functions.

Solution 21 - C++

Try to use strstr

char* lastSlash;
lastSlash = strstr(filename, ".");

Solution 22 - C++

Or you can use this:

    char *ExtractFileExt(char *FileName)
    {
        std::string s = FileName;
    	int Len = s.length();
        while(TRUE)
    	{
        	if(FileName[Len] != '.')
		        Len--;
	        else
    		{
        		char *Ext = new char[s.length()-Len+1];
	        	for(int a=0; a<s.length()-Len; a++)
		        	Ext[a] = FileName[s.length()-(s.length()-Len)+a];
    			Ext[s.length()-Len] = '\0';
        		return Ext;
	        }
    	}
    }

This code is cross-platform

Solution 23 - C++

So, using std::filesystem is the best answer, but if for whatever reason you don't have C++17 features available, this will work even if the input string includes directories:

string getextn (const string &fn) {
  int sep = fn.find_last_of(".\\/");
  return (sep >= 0 && fn[sep] == '.') ? fn.substr(sep) : "";
}

I'm adding this because the rest of the answers here are either strangely complicated or fail if the path to the file contains a dot and the file doesn't. I think the fact that find_last_of can look for multiple characters is often overlooked.

It works with both / and \ path separators. It fails if the extension itself contains a slash but that's usually too rare to matter. It doesn't do any filtering for filenames that start with a dot and contain no other dots -- if this matters to you then this is the least unreasonable answer here.

Example input / output:

/ => ''
./ => ''
./pathname/ => ''
./path.name/ => ''
pathname/ => ''
path.name/ => ''
c:\path.name\ => ''
/. => '.'
./. => '.'
./pathname/. => '.'
./path.name/. => '.'
pathname/. => '.'
path.name/. => '.'
c:\path.name\. => '.'
/.git_ignore => '.git_ignore'
./.git_ignore => '.git_ignore'
./pathname/.git_ignore => '.git_ignore'
./path.name/.git_ignore => '.git_ignore'
pathname/.git_ignore => '.git_ignore'
path.name/.git_ignore => '.git_ignore'
c:\path.name\.git_ignore => '.git_ignore'
/filename => ''
./filename => ''
./pathname/filename => ''
./path.name/filename => ''
pathname/filename => ''
path.name/filename => ''
c:\path.name\filename => ''
/filename. => '.'
./filename. => '.'
./pathname/filename. => '.'
./path.name/filename. => '.'
pathname/filename. => '.'
path.name/filename. => '.'
c:\path.name\filename. => '.'
/filename.tar => '.tar'
./filename.tar => '.tar'
./pathname/filename.tar => '.tar'
./path.name/filename.tar => '.tar'
pathname/filename.tar => '.tar'
path.name/filename.tar => '.tar'
c:\path.name\filename.tar => '.tar'
/filename.tar.gz => '.gz'
./filename.tar.gz => '.gz'
./pathname/filename.tar.gz => '.gz'
./path.name/filename.tar.gz => '.gz'
pathname/filename.tar.gz => '.gz'
path.name/filename.tar.gz => '.gz'
c:\path.name\filename.tar.gz => '.gz'

Solution 24 - C++

If you happen to use Poco libraries you can do:

#include <Poco/Path.h>

...

std::string fileExt = Poco::Path("/home/user/myFile.abc").getExtension(); // == "abc"

Solution 25 - C++

If you consider the extension as the last dot and the possible characters after it, but only if they don't contain the directory separator character, the following function returns the extension starting index, or -1 if no extension found. When you have that you can do what ever you want, like strip the extension, change it, check it etc.

long get_extension_index(string path, char dir_separator = '/') {
    // Look from the end for the first '.',
    // but give up if finding a dir separator char first
    for(long i = path.length() - 1; i >= 0; --i) {
        if(path[i] == '.') {
            return i;
        }
        if(path[i] == dir_separator) {
            return -1;
        }
    }
    return -1;
}

Solution 26 - C++

I used PathFindExtension() function to know whether it is a valid tif file or not.

#include <Shlwapi.h>
bool A2iAWrapperUtility::isValidImageFile(string imageFile)
{
	char * pStrExtension = ::PathFindExtension(imageFile.c_str());

	if (pStrExtension != NULL && strcmp(pStrExtension, ".tif") == 0)
	{
		return true;
	}

	return false;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeffVView Question on Stackoverflow
Solution 1 - C++brian newmanView Answer on Stackoverflow
Solution 2 - C++TorlackView Answer on Stackoverflow
Solution 3 - C++RazView Answer on Stackoverflow
Solution 4 - C++17 of 26View Answer on Stackoverflow
Solution 5 - C++Roi DantonView Answer on Stackoverflow
Solution 6 - C++peter karasevView Answer on Stackoverflow
Solution 7 - C++graphitemasterView Answer on Stackoverflow
Solution 8 - C++QiuView Answer on Stackoverflow
Solution 9 - C++serengeorView Answer on Stackoverflow
Solution 10 - C++manlioView Answer on Stackoverflow
Solution 11 - C++KristianRView Answer on Stackoverflow
Solution 12 - C++AardvarkView Answer on Stackoverflow
Solution 13 - C++delaccount992View Answer on Stackoverflow
Solution 14 - C++Leopoldo SanczykView Answer on Stackoverflow
Solution 15 - C++Mark KahnView Answer on Stackoverflow
Solution 16 - C++AMCodedView Answer on Stackoverflow
Solution 17 - C++Mike FinchView Answer on Stackoverflow
Solution 18 - C++HaseeB MirView Answer on Stackoverflow
Solution 19 - C++tfinnigaView Answer on Stackoverflow
Solution 20 - C++JahidView Answer on Stackoverflow
Solution 21 - C++MaadiahView Answer on Stackoverflow
Solution 22 - C++QuestView Answer on Stackoverflow
Solution 23 - C++Jason CView Answer on Stackoverflow
Solution 24 - C++Darien PardinasView Answer on Stackoverflow
Solution 25 - C++YuvalView Answer on Stackoverflow
Solution 26 - C++Pabitra DashView Answer on Stackoverflow