converting std::string to char *

c++, char, string

Solution

Use `fname.c_str()` as:

std::fstream fout (fname.c_str(), std::fstream::app | std::fstream::out);

`.c_str()`returns `const char*`. In C++03, you've to pass `const char*`.

In C++11, you can pass `std::string` but it seems your compiler doesn't support it.

Problem

Possible Duplicate: Convert std::string to const char* or char* I have a `std::string` and want to pass that as a file name to fstream like ``` std::string fname = "/home/mahmood/filter" + boost::lexical_cast<std::string>(b) + ".txt"; std::fstream fout (fname, std::fstream::app | std::fstream::out); ``` but I get an error for the second line ``` error: no matching function for call to ‘std::basic_fstream<char, std::char_traits<char> >::basic_fstream(std::string&, ``` Seems that it can not convert `string` to `char *`. Also casting doesn't work

Original source

Related problems