I have a function that experiences a hang issue on Windows 10 but runs smoothly on Linux. The problematic line of code seems to be bfs::recursive_directory_iterator it(dirPath)
. After this point, no further code execution occurs.
However, when I substitute boost::filesystem
with std::filesystem
, everything works as expected. Can anyone shed light on why this behavior is occurring?
I am using Boost version 1.82.
Here's the function in question:
#include <boost/filesystem.hpp>
#include <iostream>
#include <vector>
namespace bfs = boost::filesystem;
void listFilesRecursive(const bfs::path &dirPath, std::vector<std::string> &filePaths)
{
// Convert dirPath to a string and print it
std::string dirPathStr = dirPath.string();
std::cout << "Directory path: " << dirPathStr << std::endl;
// Check if the directory exists and is a directory
if (!bfs::exists(dirPath) || !bfs::is_directory(dirPath))
{
std::cout << "dirPath not exist" << dirPath << std::endl;
return;
}
for (bfs::recursive_directory_iterator it(dirPath), end; it != end; ++it)
{
if (bfs::is_regular_file(*it))
{
std::cout << "filepath=" << it->path().string() << std::endl;
filePaths.push_back(it->path().string());
}
}
}
int main()
{
const std::string dirPathStr = "/path/to/your/directory";
std::vector<std::string> filePaths;
listFilesRecursive(dirPath, filePaths);
return 0;
}
if recursive path is too deep then usually boost hangs and you won't be able get any error or exception from boost .I would suggest use conditional compilation to address this issue.
#ifdef _WIN64 // Check if compiling on Windows (64-bit)
#include <filesystem>
namespace fs = std::filesystem;
#else // Not compiling on Windows (64-bit), assume Linux
#include <boost/filesystem.hpp>
namespace bfs = boost::filesystem;
#endif // _WIN64
in this way on windows you can compile and executing the code using std::filesystem and on linux using boost::filesystem