Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

boost::filesystem version 3 issues

This topic is 2638 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to update my version 2 code to version 3 and can't figure out what I am missing to get boost to send in the filenames as strings?

I want the filenames in the file vector and the directories in dir vector....

Thanks!


bool NX::FILESYSTEM::GetFiles(const std::string& path, std::vector<std::string>& file, std::vector<std::string>& dir)
{
boost::filesystem::path p(path);
try
{
if(boost::filesystem::exists(p)) // does p actually exist?
{
if(boost::filesystem::is_regular_file(p)) // is p a regular file?
{
std::cout << p << " size is " << boost::filesystem::file_size(p) << '\n';
}
else if(boost::filesystem::is_directory(p)) // is p a directory?
{
std::cout << p << " is a directory containing:\n";
std::copy(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), // directory_iterator::value_type
std::ostream_iterator<boost::filesystem::directory_entry>(std::cout, "\n")); // is directory_entry, which is
// converted to a path by the
// path stream inserter
file.push_back(p.filename().string().c_str());
}
else
std::cout << p << " exists, but is neither a regular file nor a directory\n";
}
else
std::cout << p << " does not exist\n";
return true;
}
catch(const boost::filesystem::filesystem_error& ex)
{
std::cout << ex.what() << '\n';
}
return false;
}

Share this post


Link to post
Share on other sites
Advertisement
You can use the following, it will traverse the file tree recursively (won't follow symbolic links) and add the file and directory names to the appropriate vectors. It won't add the base directory (you can add it yourself and check with [font="Courier New"]boost::filesystem::is_directory[/font]) and will throw if "path" isn't a directory.

[source lang="cpp"]
void GetFiles(const std::string &path, std::vector<std::string> &file, std::vector<std::string> &dir)
{
try
{
boost::filesystem::recursive_directory_iterator it(path);
boost::filesystem::recursive_directory_iterator end;

for ( /**/ ; it != end ; ++it)
{
if (boost::filesystem::is_regular_file(*it))
{
file.push_back(it->path().generic_string());
}
else if (boost::filesystem::is_directory(*it))
{
dir.push_back(it->path().generic_string());
}
}
}
catch (boost::filesystem::filesystem_error &e)
{
std::cerr << "Error reading files: '" << e.what() << "'\n";
}
}
[/source]

Share this post


Link to post
Share on other sites

You can use the following, it will traverse the file tree recursively (won't follow symbolic links) and add the file and directory names to the appropriate vectors. It won't add the base directory (you can add it yourself and check with [font="Courier New"]boost::filesystem::is_directory[/font]) and will throw if "path" isn't a directory.

[source lang="cpp"]
void GetFiles(const std::string &path, std::vector<std::string> &file, std::vector<std::string> &dir)
{
try
{
boost::filesystem::recursive_directory_iterator it(path);
boost::filesystem::recursive_directory_iterator end;

for ( /**/ ; it != end ; ++it)
{
if (boost::filesystem::is_regular_file(*it))
{
file.push_back(it->path().generic_string());
}
else if (boost::filesystem::is_directory(*it))
{
dir.push_back(it->path().generic_string());
}
}
}
catch (boost::filesystem::filesystem_error &e)
{
std::cerr << "Error reading files: '" << e.what() << "'\n";
}
}
[/source]



Thanks a million! Works like I needed it to.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!