Question

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
}
Was it helpful?

Solution

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()

OTHER TIPS

You could use a std::ostringstream as intermediate:

std::ostringstream os;
os << di->path();

std::string path = os.str();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top