Getting a File Extension?

This is a discussion on Getting a File Extension? within the C++ Programming forums, part of the General Programming Boards category; Hi, I've writ a function that gets all files in a directory, but would like to refine this by allowing ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    41

    Getting a File Extension?

    Hi,

    I've writ a function that gets all files in a directory, but would like to refine this by allowing it to recognise file extensions.

    Unfortunatly, I can't find a way to do this - I've tried strpbrk, strrchar, and a number of other string functions, with lttle success. I either get compiler errors, or the added code fails to work as I would like..

    So, does anyone know of either a Windows function do get an extension, or a _working_ way of getting the same thing from a string?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    You should store your files names in string objects rather than character arrays. Then you can use functions such as find_last_of(). This should make it nice and easy. This link should give you everything you need to know about what the string object offers.

    http://www.cppreference.com/cppstring/index.html

    However, with a C style string, you should be able to achieve the result with something like
    Code:
    #include <iostream>
    #include <cstring>
    
    int main() {
        const char *myFile = "C:/Files/Data.Info/Owner.Data.txt";
        char *extPtr;
        
        char *find = strpbrk(myFile,".");
        while(find != NULL) {
           extPtr = find+1;
           find = strpbrk(extPtr,".");
        }
        
        std::cout << extPtr;
        
        return 0;
    }
    Last edited by SlyMaelstrom; 09-26-2006 at 11:44 PM.
    Sent from my iPadŽ

  3. #3
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    #include <iostream>
    #include <cstring>
    
    int main() {
        const char *myFile = "C:/Files/Data.Info/Owner.Data.txt";
        char *extPtr;
    
        extPtr = std::strrchr(myFile, '.');
    
        if ( extPtr )
            std::cout << ++extPtr;
    
        return 0;
    }
    Last edited by SKeane; 09-28-2006 at 03:12 AM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    SKeane, <cstring>'s strrchr() is in the std namespace, too:
    Code:
    #include <iostream>
    #include <cstring>
    
    int main() {
        const char *myFile = "C:/Files/Data.Info/Owner.Data.txt";
        char *extPtr;
    
        extPtr = std::strrchr(myFile, '.');
    
        if ( extPtr )
            std::cout << ++extPtr;
    
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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