Thread: Simple problem

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    Simple problem (solved)

    Hey guys. I have a string:

    char shortpath[1000];

    That string above is a filepath. What I want to do is figure out the files extension. In order to do this I'm guessing I would have to find the last 4 chars of the string. Then I would compare the last 4 chars with an extension like ".mp3", therefore confirming the file is an mp3. How exctly would I get those last 4 chars from it? I'm a C++ noob and I have treid for hours and not succeded. Thanks.
    Last edited by andrewmc; 10-06-2006 at 10:44 PM. Reason: solved

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I'm a C guy, so using strrchr to find the last . in the string and then using strcmp on the remaining would be my start. C++ offers similar functions (as well as containing those I suggested). Post your attempt (preferably within code tags).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    I can't believe I didn't think of that.

    char* extension=strchr(shortpath,'.');

    That sucessfully returns the file extension. Only problem is that strcmp is case sensitive. It does not see the extension ".MP3" as equal to ".mp3". Any ideas?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    You have been a HUGE help. I am forever in your debt :P

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by andrewmc
    char* extension=strchr(shortpath,'.');
    Note also that I mentioned strrchr rather than strchr.

    This may come into play if your file/directory text contains a ..
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Oops, that could have caused some problems. Thanks again for all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM