给定用户的文件路径不会工作c ++

时间:2016-01-16 18:06:52

标签: c++

我正在尝试从用户那里获取路径,然后将其插入ifstream,但它不起作用。我的文件位于:C:\test.txt我使用了双反斜杠,但它仍无效。

string pot;

cout<<"GIVE PATH TO FILE:"<<endl;
getline(cin,pot);
//pot="C:\\test.txt";
string line;
string besedilo[2];
short i=0;
ifstream myfile (pot.c_str());
if (myfile.is_open())
{
    while ( getline (myfile,line) )
    {
        besedilo[i]=line;
        i=i+1;
    }
    myfile.close();
}

2 个答案:

答案 0 :(得分:1)

我已经尝试过你的代码了...它在我的D:盘上的.txt文件中运行得很好。 请确保您使用双斜杠并正确编写路径。我没有看到任何其他选项,为什么它不能像你想要的那样工作。

答案 1 :(得分:0)

您可以使用Boost Filesyetm。它与平台无关,可以处理所有反斜杠,正斜杠,逃避斜线的头痛。