Search code examples
c++linuxc++11type-conversionnon-ascii-characters

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?


Solution

  • 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.