将const char *文件的地址传递给ifstream对象的问题

时间:2015-03-12 21:08:53

标签: c++ string oop ifstream

ifstream对象采用const char *。我给我的ifstream对象是我文件的路径

ifstream in_file("E:\\myfile.csv"); //File opens

如果我想要传递一个包含我的文件地址的字符串,我必须首先将它转换为const char *,我这样做:`

string mystr="\"E:\\\\myfile.csv\"" //mystr contains address 
const char* c = mystr.c_str();      //conversion of std::string to const char*

问题出在这里,

in_file(c);                      //cout << c; gives E:\\myfile.csv

我在上面的步骤中遇到错误......无法弄清楚出了什么问题。我在Windows上工作

编辑:我没有收到错误,文件只是没有加载

if (in_file.is_open())
    cout << "opened";
else
    cout << "not opened";

我得到“没有打开”

1 个答案:

答案 0 :(得分:0)

使用

string mystr="E:\\myfile.csv";

而不是

string mystr="\"E:\\\\myfile.csv\"";