Reputation: 621
I want to get the list of all files in a directory using boost::filesystem
I'm able to print the filenames using cout
but i'm not able to store the filenames in a string variable. I have also tried type-casting and strcpy but none of the methods is working.
Following is the code :
char dir[100] = "/home/harsh/";
namespace fs = boost::filesystem;
fs::directory_iterator start = fs::directory_iterator(dir);
fs::directory_iterator di = start;
for (; di != fs::directory_iterator(); ++di)
{
std::cout << "hello .. " << di->path() << std::endl;
//std::string strHarsh = di->path(); //Error
}
Upvotes: 1
Views: 2679
Reputation: 409136
You could use a std::ostringstream
as intermediate:
std::ostringstream os;
os << di->path();
std::string path = os.str();
Upvotes: 1
Reputation: 13892
try di->leaf()
it should convert to string
Also it depends on your version of boost, if you are using filesystem v3 it will be:
di->path().filename().string()
Upvotes: 5