finding the extension of a file

This is a discussion on finding the extension of a file within the C++ Programming forums, part of the General Programming Boards category; I need to the find the extension of a file if i have the full filename in a sting. To ...

  1. #1
    Unregistered
    Guest

    finding the extension of a file

    I need to the find the extension of a file if i have the full filename in a sting.


    To be more specific, if i have a filename such as file.exe, i need to know if it ends in .cpp


    the only thing i can think of is to trim the string leaving on the last 4 characters, but i dont think thats possible in c++.


    Any ideas?

  2. #2
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    take the string in as a character array

    then take all characters after the period and place them into a new array.

    Compare to the string literal you are looking for....
    Blue

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If using character arrays, try this:
    Code:
    #include <cstring>
    
    using namespace std;
    int main(void)
    {
       char extension[5] = "";
       char *filename = "dictionary.cpp";
    
       strncpy(extension,filename+strlen(filename)-4,4);
       cout << "extension:" << extension << endl;
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21