如何将字符串转换为 ifstream
我正在尝试使用 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。
i am trying to open a file with ifstream and i want to use a string as the path (my program makes a string path). it will compile but it stays blank.
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;
}
}
I am using windows 7, My compiler is VC++ 2010.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试过
ifstream myfile(path.c_str());
?请参阅关于
while (!不管怎样。eof())
。Have you tried
ifstream myfile(path.c_str());
?See a previous post about the problems with
while (!whatever.eof())
.我不确定这实际上是如何编译的,但我假设您正在寻找:
I'm unsure as to how this actually compiles, but I assume you are looking for: