【问题标题】:converting std::string to char * [duplicate]将 std::string 转换为 char * [重复]
【发布时间】:2012-10-31 02:05:21
【问题描述】:

可能重复:
Convert std::string to const char* or char*

我有一个std::string 并想将其作为文件名传递给 fstream 之类的

std::string fname = "/home/mahmood/filter" + boost::lexical_cast<std::string>(b) + ".txt";
std::fstream fout (fname, std::fstream::app | std::fstream::out);

但第二行出现错误

error: no matching function for call to ‘std::basic_fstream<char, std::char_traits<char>   >::basic_fstream(std::string&, 

似乎无法将string 转换为char *。投射也不起作用

【问题讨论】:

  • google会这么难吗?

标签: c++ string char


【解决方案1】:

fname.c_str() 用作:

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

.c_str()返回const char*。在 C++03 中,您必须传递 const char*

在 C++11 中,您 can pass std::string 但您的编译器似乎不支持它。

【讨论】:

    猜你喜欢
    • 2013-09-10
    • 1970-01-01
    • 2014-07-13
    • 2010-11-14
    • 2014-03-17
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多