Cutting up an std::string

This is a discussion on Cutting up an std::string within the C++ Programming forums, part of the General Programming Boards category; Okay, so I've ran into a problem, it is great that the .ms3d file gives me a file path for ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Cutting up an std::string

    Okay, so I've ran into a problem, it is great that the .ms3d file gives me a file path for another file I need to draw the ms3d file (a texture)..

    But it turns out, that that texture filename is relative to the location it was origionally created!!!

    BAD!!

    So no matter what I do, when I load the file it spits out an arbitrary file path that I don't have, but there is still good news...


    It does have the final file name in it

    asdf.bmp or whatever it may be...

    This is good, that means if I can somehow cut the filename down to the last /asdf.bmp part, and then add my own filepath/textures part to it, I can successfully load any file out of any path I choose...

    Is there any way to parse an std::string like this?

    If so can someone give me a nudge in the right direction?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Just a nudge? Use the string's find_last_of member function to find the last slash/backslash character. Check that this index is not equal to string::npos and continue if so. Call the string's erase member function to erase all the characters in the string from 0 up to this index.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Yeah thats really all I was looking for, a few function names to look up..

    MSDN here I come!
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Another note, how do I set a character array equal to a string?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    how do I set a character array equal to a string?
    Use c_str() to get a cstring from string and use strcpy(). Or, you can use the STL copy() algorithm.
    Last edited by 7stud; 03-31-2006 at 09:27 AM.

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I'm sorry, I ment an std::string equal to a character array! :\
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I'm sorry, I ment an std::string equal to a character array!
    Just do it.

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    with an equals sign?

    ?

    Sorry if I'm being stupid lol...

    Here is the code I don't have working...

    TextureFilename is an std::string...
    Code:
    		Materials[i].TextureFilename = new char[strlen( pMaterial->m_texture )+1];
    		strcpy( Materials[i].TextureFilename, pMaterial->m_texture );
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  9. #9
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    with an equals sign?
    yes.

    Try writing the simplest program you can that does nothing but copy a char array into a string. All will become clear.
    There is a difference between tedious and difficult.

  10. #10
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    That code I posted above should work :\

    If you're saying all I need is an equals sign, then setting TextureFilename = the new character array m_texture should be no problem..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) A string does not have a fixed size, so setting a string equal to a char pointer to an empty char array does nothing:

    Materials[i].TextureFilename = new char[strlen( pMaterial->m_texture )+1];

    2) Look up strcpy() and see what parameter types it can take:

    strcpy( Materials[i].TextureFilename, pMaterial->m_texture );

    3) Read my second post again.
    Last edited by 7stud; 03-31-2006 at 09:57 AM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    If you're saying all I need is an equals sign, then setting TextureFilename = the new character array m_texture should be no problem..
    Yes, but it isnt new... just the actual C string.
    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

  13. #13
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Alright, maybe I'm just being a little slow, can I get a couple more pushes?



    I'm trying to make it so this

    Code:
    		Materials[i].TextureFilename = new char[strlen( pMaterial->m_texture )+1];
    		strcpy( Materials[i].TextureFilename, pMaterial->m_texture );
    Copies a modified m_texture... Basically, the file I'm dealing with spits out an arbitrary path (relative upon creation), so I can do automated texture loading, well, I need to cut off the file path and add my own, so It can find the files it needs!..

    For some reason I can't make TextureFilename an std::string without complications, right now it is a character array, it needs to be an std::string!

    m_texture is a character array, strcpy is trying to copy m_texture into TextureFilename...

    Uhg, help? :\

    I tried this
    Code:
    		Materials[i].TextureFilename(new char[strlen( pMaterial->m_texture )+1]);
    As far as I can tell this works properly and is creating a basic string object out of the newchar[strlen(pMaterial->m_texture)+1); bit of code...

    But then, why does this load function I'm using set one string = to another, but then go ahead and use strcpy too, aren't they the same thing?
    Last edited by Shamino; 04-02-2006 at 07:19 AM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  14. #14
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Quote Originally Posted by Shamino
    I can't make TextureFilename an std::string without complications, right now it is a character array, it needs to be an std::string!
    What complications? If you need a std::string, use one and fix the complications; don't try to make the char array work if you can't/don't want to use one.
    There is a difference between tedious and difficult.

  15. #15
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    The file definitions hold a char array (the file def of the file i'm trying to load..)

    Or can I change that?

    My MS3D Structure can have an std::string texturename in it just fine, but I can't change the file def, which is a char array...

    So I gotta set my std::string in my ms3d structure = to that of the character array in the file def.

    And I'm having issues doing that...
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 02:34 PM
  2. std::string: Has my compiler gone nuts??
    By Andruu75 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2007, 04:02 AM
  3. Improving my code
    By rwmarsh in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2006, 11:18 AM
  4. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM
  5. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM

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