需要帮助才能进入 ifstream::open 以打开文件并从中获取行
我只是想打开这个文件并使用 getline 函数从文件中读取,但我似乎无法弄清楚为什么它不起作用。我已经多次执行它,并且 fileOpen 变量正在与我尝试打开的文件一起正确加载,因此我不确定为什么它无法打开,以便使用 getline 。我只是希望能够使用 getline 读取文件,所有这些都是在递归函数中完成的,以最终读取目录中的所有文件。如果您需要有关我到底在做什么的更多信息,请告诉我。
string line;
ifstream file;
string fileOpen;
bf::directory_iterator dirIter ( fullPath ); //fullPath is type bf::path, passed into the function
fileOpen = (dirIter->path().filename());
file.open(fileOpen);
getline(file, line);
I am just trying to open this file and use the getline function to read from the file but I cant seem to figure out why it is not working. I have stepped through it many times and the fileOpen variable is being loaded correctly with the file im trying to open, so Im unsure on why it wont open, to use getline with it. I would just like to be able to read through the file with getline, all of this is done in a recursive function to eventually read through all the files in directories. Let me know if you need more information on what exactly im doing.
string line;
ifstream file;
string fileOpen;
bf::directory_iterator dirIter ( fullPath ); //fullPath is type bf::path, passed into the function
fileOpen = (dirIter->path().filename());
file.open(fileOpen);
getline(file, line);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
path::filename
函数返回基本文件名。如果路径为“foo\bar.txt”,path::filename
将返回“bar.txt”。因此,除非“foo\”位于当前目录中,否则该文件可能不存在。您更可能寻找的是:
或者,您可以使用 boost::filesystem iostream 类型:
The
path::filename
function returns the base filename. If you have a path of "foo\bar.txt",path::filename
will return "bar.txt". So unless "foo\" is in the current directory, the file probably doesn't exist.What you're more likely looking for is this:
Or, you can use the boost::filesystem iostream types: