Getting file extension in C

58,364

Solution 1

const char *get_filename_ext(const char *filename) {
    const char *dot = strrchr(filename, '.');
    if(!dot || dot == filename) return "";
    return dot + 1;
}

printf("%s\n", get_filename_ext("test.tiff"));
printf("%s\n", get_filename_ext("test.blah.tiff"));
printf("%s\n", get_filename_ext("test."));
printf("%s\n", get_filename_ext("test"));
printf("%s\n", get_filename_ext("..."));

Solution 2

Find the last dot with strrchr, then advance 1 char

#include <stdio.h> /* printf */
#include <string.h> /* strrchr */

ext = strrchr(filename, '.');
if (!ext) {
    /* no extension */
} else {
    printf("extension is %s\n", ext + 1);
}

Solution 3

You can use the strrchr function, which searches for the last occurrence of a character in a string, to find the final dot. From there, you can read off the rest of the string as the extension.

Solution 4

Here is a version which works also for file (or directory) paths:

#include <assert.h>
#include <string.h>

const char *FileSuffix(const char path[])
{
    const char *result;
    int i, n;

    assert(path != NULL);
    n = strlen(path);
    i = n - 1;
    while ((i >= 0) && (path[i] != '.') && (path[i] != '/') & (path[i] != '\\')) {
        i--;
    }
    if ((i > 0) && (path[i] == '.') && (path[i - 1] != '/') && (path[i - 1] != '\\')) {
        result = path + i;
    } else {
        result = path + n;
    }
    return result;
}


int main(void)
{
    assert(strcmp(FileSuffix("foo/bar.baz.qux"), ".qux") == 0);
    assert(strcmp(FileSuffix("foo.bar.baz/qux"), "") == 0);
    return 0;
}
Share:
58,364
errorhandler
Author by

errorhandler

Updated on December 24, 2020

Comments

  • errorhandler
    errorhandler over 3 years

    How do you get a file extension (like .tiff) from a filename in C?

    Thanks!

  • visual_learner
    visual_learner about 13 years
    After dot = strrchr(filename, '.');, dot != NULL implies that *dot == '.' so your test ought to be simply if(dot).
  • errorhandler
    errorhandler about 13 years
    I'm getting an error: renamer.c:11: warning: incompatible implicit declaration of built-in function ‘strrchr’
  • ThiefMaster
    ThiefMaster about 13 years
    There's no need to copy it - you'll return a part of the string which ends with the end of the original string, so no modifications are necessary.
  • Steve Jessop
    Steve Jessop about 13 years
    For most practical purposes, if the last dot is the first character of the basename, then it should not be treated as a file extenstion. For example in ~/.forward, ".forward" isn't an extension.
  • errorhandler
    errorhandler about 13 years
    That doesn't really matter... because I'm just using it batch rename my photos.
  • mabalenk
    mabalenk over 4 years
    Once you have extracted the file extension a useful addendum might be the strcmp() routine from string.h. It will allow you to compare the file extension with a given string and perform a certain action, e.g. if (strcmp(ext,"tiff") == 0) ...