Thread: Couple questions

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    8

    Couple questions

    I searched the web for these, but couldn't find anything.

    1) How do you find a file's extention (type)?
    2) How do you link strings together in one variable or string?
    (eg. newstring = oldstring1 + oldsting2 [is it even possible?])

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    In C++, you can do #2 with the std::string datatype.

    For #1, just start at the end of a filename string and step back until you get a fullstop or the beginning of the string.

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
         string s1 = ("hello");
         string s2 = ("world");
         string s3 = s1 + " " + s2 + "!";
         cout << s3 << endl;
         cin.get();
         return 0;
    }

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    8
    I found strcat() to solve my problem.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I found strcat() to solve my problem.
    Then you're not really using C++ to it's best advantage - check ILoveVectors post.

    > 1) How do you find a file's extention (type)?
    Find the last '.' in the filename?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    >1) How do you find a file's extention (type)?

    something like this

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(){
    
                 string filename = "blah.mp3";
    
                 string::iterator i = filename.end();
    
                 while ( *i != '.' ){
    
                           i--;
    
                 }
    
                 cout << "The extension is " << string( i, filename.end() );
    
                 return 0;
    
         }
    Last edited by dra; 08-19-2005 at 02:24 AM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    dra, your code derefences the invalid iterator returned by end(). You would want to decrement it first. Also, you should use rfind() instead of your own hand-written loop to avoid small mistakes like that.

  8. #8
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    oh. lol okay thanks.
    Last edited by dra; 08-19-2005 at 07:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of Questions About Classes
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2009, 02:50 PM
  2. Couple of Questions
    By toonlover in forum Windows Programming
    Replies: 10
    Last Post: 07-14-2006, 01:04 AM
  3. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 07:55 AM
  4. A couple of questions that someone might know the answer to...
    By Finchie_88 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-15-2005, 08:26 AM
  5. Studying for Final, Couple Questions...
    By stewade in forum C Programming
    Replies: 4
    Last Post: 05-10-2004, 05:02 PM