需要帮助才能进入 ifstream::open 以打开文件并从中获取行

发布于 2024-11-16 03:03:59 字数 461 浏览 0 评论 0原文

我只是想打开这个文件并使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那小子欠揍 2024-11-23 03:03:59

path::filename 函数返回基本文件名。如果路径为“foo\bar.txt”,path::filename 将返回“bar.txt”。因此,除非“foo\”位于当前目录中,否则该文件可能不存在。

您更可能寻找的是:

file.open(dirIter->path().native());

或者,您可以使用 boost::filesystem iostream 类型:

#include <boost/filesystem/fstream>

bf::ifstream file;
bf::directory_iterator dirIter ( fullPath ); //fullPath is type bf::path, passed into the function
file.open(dirIter->path());

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:

file.open(dirIter->path().native());

Or, you can use the boost::filesystem iostream types:

#include <boost/filesystem/fstream>

bf::ifstream file;
bf::directory_iterator dirIter ( fullPath ); //fullPath is type bf::path, passed into the function
file.open(dirIter->path());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文