How to get file extension from string in C++

前端 未结 25 2331
迷失自我
迷失自我 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:18

    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.

    0 讨论(0)
  • 2020-11-30 23:19

    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.

    0 讨论(0)
  • 2020-11-30 23:20

    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;
      }
    }
    
    0 讨论(0)
  • 2020-11-30 23:20

    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.

    0 讨论(0)
  • 2020-11-30 23:20

    Actually, the easiest way is

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

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

    0 讨论(0)
  • 2020-11-30 23:20

    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

    0 讨论(0)
提交回复
热议问题