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

Pages: 1-

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 13:30

>>1
Is there any way of using C's string's with old C functions
using *C++'s* strings with old C functions

Typo.

Name: Anonymous 2012-02-15 13:30

Have you tried firstName.c_str()?

Name: Anonymous 2012-02-15 13:32

Pro bono

Name: Anonymous 2012-02-15 13:33

>>3
Have you tried firstName.c_str()?
Yeah, that doesn't work either.

Name: Anonymous 2012-02-15 13:38

Is there any way of using C's string's with old C functions
you really shouldn't. c_str returns a const array and it shouldn't me modified.

why don't you use just char[]?

Name: Anonymous 2012-02-15 13:43

>c_str()
jeebooze that's some disgusting function name

Name: Anonymous 2012-02-15 13:44

>>6
why don't you use just char[]?
I'd like to avoid using char arrays if possible.

Is that really the only option. I guess I could use C arrays if I have to.

Name: Anonymous 2012-02-15 13:45

>>1
First of all, using strcat as an example makes you look borderline retarded, and on the wrong side of the border, at that.

Second, for functions like GetSystemDirectory .c_str() will work fine.

If you want to use functions which expect a buffer, you have two options. You can be a good boy and malloc or statically allocate a buffer which you then assign/return as an std::string.

Or you can depend on the way all existing implementations work, and on the wording of C++11 standard which kind of more or less seems to make this legal from now on, and do
buffer.resize(1024, '\0');
hr = GetFullPathName(path.c_str(), buffer.size(), &buffer[0], NULL);


Though I must add that if despite of your alleged C background you couldn't understand what's the problem with your code then you probably are not born to be a programmer and should consider a career in sucking dicks instead.

Name: >>9 2012-02-15 13:47

Forgot to add, then you should check the return value and if it's OK, shrink the buffer back to it.

Name: Anonymous 2012-02-15 13:48

>>8
I'd like to avoid using char arrays if possible.
I'd like to avoid using the functions designed to work on string, replacement for char arrays.
Are you an idiot?

Name: Anonymous 2012-02-15 13:57

Second, for functions like GetSystemDirectory .c_str() will work fine.


int main(void)
{
    string systemPath;
    GetSystemDirectory(systemPath.c_str(), systemPath.size());
}


This doesn't work. [code]c_str()[code] returns a [code]const char*[code]. Is there something I'm missing (aside from a brain?).

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.

Name: Anonymous 2012-02-15 18:47

Why not just work with string where you can, and just strcpy(my_char_array, my_cpp_string) wherever you need it in char * format?  It will be inefficient as fuck, but you get what you pay for when you don't have the balls to either stick with C or convert to C++ completely.

Name: Anonymous 2012-02-15 18:48

>>14
Woops, that should be strcpy(my_char_array, my_cpp_string.c_str()).

Name: Anonymous 2012-02-15 20:23

>>1
string SepplesGetSystemDirectory() {
     char buffer[MAX_PATH+1];
     GetSystemDirectory(buffer, MAX_PATH+1);
     return new string(buffer);
}

Name: Anonymous 2012-02-16 0:42

the

Name: Anonymous 2012-02-16 4:40

>>13
That is definitely non-standards compliant, because no C++ standard guarantees that the terminating zero is stored with the string, while strcat does write it. So you have to do
firstName.resize(firstName.length() + lastName.length() + 1, '\0');

Name: Anonymous 2012-02-16 10:18

int main(void)
{
    char * firstName = "John";
    char * lastName = "Doe";
  
    //This does compile.
    strcat(firstName, lastName);
    //This compiles and does work.
    strcat(&firstName[0], lastName);
}

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