0

I'm trying to create a method that can get the extension of a file. I found multiple alternatives and this one was my solution:

const char *getExt(const char *filename) {
    const char *ext = strrchr(filename, '.');
    if(!ext || ext == filename){
        return "No Extension";
    }else{
        return ext + 1;
    }
}

Now, the problem is with the extension .tar.gz.

How can I select this kind of extension too without messing every other extension with only one dot (.)?

Jongware
  • 22,200
  • 8
  • 54
  • 100

1 Answers1

0

In use by me for many years:

char *GetExtension (const char *str)                // Extract the extension from the string  (incl '.')
{
    const char *pstr;
    if (!str ||!*str) return((char *)str);

    pstr = str + strlen(str) - 1;
    while ((*pstr != '/') && (*pstr != '\\') && (*pstr != '.') && (pstr > str))
            pstr--;
    if (*pstr != '.') return (0);
    return ((char *)pstr); //includes separator
}
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41