Removing file extentions from ansistrings

This is a discussion on Removing file extentions from ansistrings within the C++ Programming forums, part of the General Programming Boards category; I'm trying to create a spelling program that slects an audio file plays it and then compares the input in ...

  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,077
    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 12:06 PM. Reason: For the filenaming challenged
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    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,077
    Ick, who names their files like that?
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    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
    22,264
    Ick, who names their files like that?
    I have seen some people name their files "filename.inc.php"....
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01:36 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01: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, 11: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

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