Dave's Brain

Browse - Programming Tips - How can I get the extension of a filename?

Date: 2013dec19
Language: C/C++
Keywords: suffix

Q.  How can I get the extension of a filename?

A.

char *GetExtensionSimple(const char *filename) { char *p; if (filename == NULL) return NULL; if ((p = strrchr(filename, '.')) == NULL) return &filename[lstrlen(filename)]; return p; }
This handles a NULL filename, or a filename with no extension. There are also library routines for this - but this works everywhere.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.