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

前端 未结 3 2008
自闭症患者
自闭症患者 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:30

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

提交回复
热议问题