Thread: file extension

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Lithuania
    Posts
    11
    Rep Power
    0

    file extension


    Hi,

    I need function to split extension from filename in C

    For example:

    "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.

    Bye.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    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.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Lithuania
    Posts
    11
    Rep Power
    0
    no, it seems it does'nt have splitpath()

    Im using GCC
    (Thread model: posix
    gcc version 3.3.1 20030626 (Debian prerelease))
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    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:
    Code:
    // calling code must provide the ext buffer to receive the extension
    char *GetFileExt(char *filename, char *ext)
    {
        int    i;
    
        for (i=strlen(filename)-1; i >= 0 && filename[i] != '.'; i--);
        if (i < 0)   // period not found, hence no extension
            strcpy(ext,"");
        else
            strcpy(ext,&filename[i+1]);
    
        return ext;
    }
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Lithuania
    Posts
    11
    Rep Power
    0
    thanks!
    this one works very good :)
  10. #6
  11. Doggie
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    Seattle, WA
    Posts
    751
    Rep Power
    13
    or you could use:
    Code:
    CString s,ext;
    
    s="myfile.txt";
    ext=s.Mid(s.ReverseFind('.'));
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Originally posted by dog135
    or you could use:
    Code:
    CString s,ext;
    
    s="myfile.txt";
    ext=s.Mid(s.ReverseFind('.'));
    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.
  14. #8
  15. Doggie
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    Seattle, WA
    Posts
    751
    Rep Power
    13
    Oops, you're right. I got my files mixed up when I was looking up info. This should be standard:

    Code:
    #include <iostream>
    using namespace std;
    #include <string>
    
    void main()
    {
    	string s,ext;
    	s="myfile.txt";
    	ext=s.substr(s.rfind('.')+1);
    	cout << ext;
    }
    Last edited by dog135; August 1st, 2003 at 02:41 PM.

IMN logo majestic logo threadwatch logo seochat tools logo