Thread: Removing file extentions from ansistrings

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    8

    Removing file extentions from ansistrings

    I'm trying to create a spelling program that slects an audio file plays it and then compares the input in a txtbox to check if the filename matches the input. at the moment however the filename still contains the .wav extention at the end. Any ideas how to remove this would be gratefully appreciated

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    #include <iostream>
    #include <string>
    
    std::string removeExt(std::string);
    
    int main() {
        std::string filename = "mymusic.wav";
        filename = removeExt(filename);
        
        std::string filename2 = "myfriends.music.mpeg";
        filename2 = removeExt(filename2);
        
        std::cout << filename << std::endl << filename2;
        std::cin.get();
        return 0;
    }
    
    std::string removeExt(std::string filename) {
        int extPos = filename.rfind(".",filename.size() - 1);
        filename = filename.substr(0,extPos);
        return filename;
    };
    Output:
    Code:
    mymusic
    myfriends.music
    Last edited by SlyMaelstrom; 04-12-2006 at 11:06 AM. Reason: For the filenaming challenged
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    What if the filename is something like "test.wav.mp3.mpeg", then your solution would fail. Try find_last_of instead of find.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ick, who names their files like that?
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Hopefully nobody lol, but who knows...
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ick, who names their files like that?
    I have seen some people name their files "filename.inc.php"....
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM