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

need help to getin ifstream::open to open a file and getline from it

本文关键字:获取 文件 帮助 ifstream open      更新时间:2023-10-16

我只是想打开这个文件,并使用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);

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

你更可能要找的是:

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

或者,您可以使用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());