Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ string question

Name: Anonymous 2012-02-15 13:28

I've avoided C++ for a long time but after a few months of using two high level languages (PHP and Javascript), I can't productively rely on C's strings any more. So I have a question. Is there any way of using C's string's with old C functions like strcat() and strcpy(). Yeah, I'm aware of the C++ equivalents.


int main(void)
{
    string firstName = "John";
    string lastName = "Doe";
   
    //This doesn't compile.
    strcat(firstName, lastName.c_str());
    //This compiles but doesn't work.
    strcat(&firstName[0], lastName.c_str());
}


Note: I'm aware that firstName += lastName would work. Before migrating to C++ I have to find a way to get C++ strings to work with C functions. For example, in the Windows API there's a function called GetSystemDirectory(char * path, int size) which sets the variable 'path' with the path of the system directory. If possible I don't want to have to create C arrays to deal with functions like that. Thanks in advance.

Name: Anonymous 2012-02-15 14:10

OP here, I used to be less inept with C before I whored myself with PHP.

>>9

#include <iostream>
#include <string>
using namespace std;

int main(void)
{
    string firstName = "John";
    string lastName = "Doe";
    firstName.resize(firstName.length() + lastName.length(), '\0');
    strcat(&firstName[0], lastName.c_str());
    cout << firstName;
    return 0;
}


Thank you for your solution.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List