the example code on the boost website is not working. http://www.boost.org/doc/libs/1_46_1/libs/filesystem/v3/doc/tutorial.html#Using-path-decomposition
int main(int argc, char* argv[])
{
path p (argv[1]); // p reads clearer than argv[1] in the following code
try
{
if (exists(p)) // does p actually exist?
{
if (is_regular_file(p)) // is p a regular file?
cout << p << " size is " << file_size(p) << '\n';
else if (is_directory(p)) // is p a directory?
{
cout << p << " is a directory containing:\n";
typedef vector<path> vec; // store paths,
vec v; // so we can sort them later
copy(directory_iterator(p), directory_iterator(), back_inserter(v));
sort(v.begin(), v.end()); // sort, since directory iteration
// is not ordered on some file systems
for (vec::const_iterator it (v.begin()); it != v.end(); ++it)
{
path fn = it->path().filename(); // extract the filename from the path
v.push_back(fn); // push into vector for later sorting
}
}
else
cout << p << " exists, but is neither a regular file nor a directory\n";
}
else
cout << p << " does not exist\n";
}
catch (const filesystem_error& ex)
{
cout << ex.what() << '\n';
}
return 0;
}
two error messages when compiled in visual studio 2010 for the line path fn = it->path().filename();
The first error is: 'function-style cast' : illegal as right side of '->' operator and the second error is: left of '.filename' must have class/struct/union
Also, when I mouse over path() it says: class boost::filesystem3::path Error: typename not allowed
This section (
for‘s body) is problematic:itpoints topathobjects, so I don’t see whypath()is called. Seems that it should be replaced withit->filename()EDIT: Looking at the original example I see that these are your modification. If you want to store filenames instead of printing them, define another vector of
stringorpathand store filenames in it, don’t reuse the first one. And removing the call topath()should solve the compilation problem.EDIT 2: as a cute BTW, you can achieve both directory traversal and filename extraction in one pass using
std::transforminstead ofstd::copy:Same using
mem_fun_refinstead offnameExtractorfunctor: