C++ ifstream error using string as opening file path.

前端 未结 3 1981
自闭症患者
自闭症患者 2020-11-30 21:50

I have:

string filename: 
ifstream file(filename);

The compilers complains about no match between ifstream file and a string. Do I need to

相关标签:
3条回答
  • 2020-11-30 22:15

    in c++-11 it can also be an std::string. So (installing c++-11 and) changing the dialect of you project to c++-11 could also fix the problem.

    0 讨论(0)
  • 2020-11-30 22:30

    The ifstream constructor expects a const char*, so you need to do ifstream file(filename.c_str()); to make it work.

    0 讨论(0)
  • 2020-11-30 22:37

    Change

    ifstream file(filename);
    

    to

    ifstream file(filename.c_str());
    

    Because the constructor for an ifstream takes a const char*, not a string pre-C++11.

    0 讨论(0)
提交回复
热议问题