【发布时间】:2011-02-02 23:36:00
【问题描述】:
我正在尝试使用 ifstream 打开一个文件,并且我想使用一个字符串作为路径(我的程序创建了一个字符串路径)。它会编译,但它保持空白。
string path = NameOfTheFile; // it would be something close to "c:\file\textfile.txt"
string line;
ifstream myfile (path); // will compile but wont do anything.
// ifstream myfile ("c:\\file\\textfile.txt"); // This works but i can't change it
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
}
我使用的是 windows 7,我的编译器是 VC++ 2010。
【问题讨论】: