文件名创建Ifstream对象时出错

时间:2018-05-08 10:13:59

标签: c++ ifstream

为什么这会失败?

    std::string r = "Hej.whatever";
    std::string const *p = &r;
    std::ifstream file ((*p).c_str(), std::ios::in, std::ios::ate);
    std::ifstream file ((*p), std::ios::in, std::ios::ate);
error: no matching function for call to
  ‘std::basic_ifstream<char>::basic_ifstream(const string&,
               const openmode&, const openmode&)’
  std::ifstream file ((*p), std::ios::in, std::ios::ate);

1 个答案:

答案 0 :(得分:0)

您需要按位OR(|)传递几种模式:

std::ifstream file ((*p), std::ios::in | std::ios::ate);
std::ifstream file2 ((*p), std::ios::in | std::ios::ate);

您可以从第一次通话中删除.c_str(),但不需要