enedil
enedil

Reputation: 1645

Non-ASCII characters - conversion from std::string to char*

I have a string with non-ASCII characters, for example std::string word ("żółć"); or std::string word ("łyżwy"); I need to convert it properly to const char * in order to call system(my_String_As_A_Const_Char_Pointer);

I'm working on Linux.

How can I do it?

Upvotes: 0

Views: 1632

Answers (2)

RichardPlunkett
RichardPlunkett

Reputation: 2988

With these conversions the only thing to care about is mixing wide chars with normal chars (which fails horribly). You are using a std:string, so c_str() is fine for pulling out a const char* to pass to some other library call.

Upvotes: 1

Shoe
Shoe

Reputation: 76300

You can use the std::string::c_str member function. It will return a const char * that can be used in functions that accept that type of argument. Here's an example:

int main(int, char*[]) {
    std::string word("żółć");
    const char* x = word.c_str();
    std::cout << x;
}

And here's a live example.

Upvotes: 4

Related Questions