2

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

1
  • 2
    Would it be so hard to google?
    – user529758
    Nov 11, 2012 at 10:38

1 Answer 1

9

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.