July 30th, 2003, 02:28 PM
I need function to split extension from filename in C
"asdas as asdf sadf asdf asdf.class" - ext.: "class"
"asdfasdf.sadfasdfgh.dsfsadf.mp3" - ext.: mp3
Maybe someone have already written this function ? :)
Thanks for answers.
July 30th, 2003, 02:54 PM
Your compiler might already have one.
E.g., _splitpath in Visual C++ and in Borland's Turbo C++. The function is apparently a kind of DOS/Windows standard.
July 30th, 2003, 03:23 PM
no, it seems it does'nt have splitpath()
Im using GCC
(Thread model: posix
gcc version 3.3.1 20030626 (Debian prerelease))
July 30th, 2003, 03:43 PM
I would think that setting a pointer to the end of the filename string, then decrementing it forward until it encounters a period, then incrementing once to point to the first char of the extension and strcpy that.
Off the top of my head:
// calling code must provide the ext buffer to receive the extension
char *GetFileExt(char *filename, char *ext)
for (i=strlen(filename)-1; i >= 0 && filename[i] != '.'; i--);
if (i < 0) // period not found, hence no extension
July 30th, 2003, 03:51 PM
this one works very good :)
July 30th, 2003, 05:39 PM
or you could use:
July 30th, 2003, 06:59 PM
I don't think so, since that is MFC and he's using gcc on Debian (if I read that correctly).
However, my offering was straight C, but if he's using C++, then the string class in the Standard Library might have a method similar to your offering. I simply don't know, since all my C++ training and most of my experience predate the new standard.
Last edited by dwise1_aol; July 30th, 2003 at 07:03 PM.
August 1st, 2003, 02:34 PM
Oops, you're right. I got my files mixed up when I was looking up info. This should be standard:
using namespace std;
cout << ext;
Last edited by dog135; August 1st, 2003 at 02:41 PM.