Concatenate chars and std::strings.

This is a discussion on Concatenate chars and std::strings. within the C++ Programming forums, part of the General Programming Boards category; I have an std::string variable called filename, then a const char* name as a parameter of a function, a char* ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Concatenate chars and std::strings.

    I have an std::string variable called filename, then a const char* name as a parameter of a function, a char* directory and then I also want to add the string "\testing\", how can I concatenate directory + "\testing\" + name?
    I was doing filename = *directory + "/textures/" + *name; but it the value of filename remains "".

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    BTW, I'm using MSVS 2003 with it's implementation of STL.

  3. #3
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    filename = directory + string("textures") + string(name);

    Note that the first cast to string is optional as the compiler will detect that it needs to cast to string itself. The second and subsequent (unless you interleave real strings) will need the explicit cast.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    God... I'm such a retard sometimes... Thanks mitakeet.

Popular pages Recent additions subscribe to a feed

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