boost/filesystem runtime error
category: general [glöplog]
I'm working on coding something that uses boost. I found an example of directory searching in O'Reilly C++ Cookbook that looked like this.
This code compiles and runs fine... But my adaptation, on the other hand, is crashing at runtime. Here is a snippit of my code.
I'm having trouble finding out why this is crashing, and how to get it running. A couple things I know are, is_directory() works with literals, for instance, if I were to say is_directory("C:\\WINDOWS"); it would return true. But if I pass it a string with that value, wont go. Also, it'll work with the boost type path. So, for example, if I pass it is_directory(fullPath); it wont crash, but will display all paths, naturally.
*it is the path iterator, so, sending the arg *it would seem to be sending a single element of fullPath, but it just doesent work... :S
Any ideas?
Code:
#include <iostream>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
using namespace boost::filesystem;
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " [dir name]\n";
return(EXIT_FAILURE);
}
path fullPath = // Create the full, absolute path name
system_complete(path(argv[1], native));
if (!exists(fullPath)) {
std::cerr << "Error: the directory " << fullPath.string( )
<< " does not exist.\n";
return(EXIT_FAILURE);
}
if (!is_directory(fullPath)) {
std::cout << fullPath.string( ) << " is not a directory!\n";
return(EXIT_SUCCESS);
}
directory_iterator end;
for (directory_iterator it(fullPath);
it != end; ++it) { // Iterate through each
// element in the dir,
std::cout << it->leaf( ); // almost as you would
if (is_directory(*it)) // an STL container
std::cout << " (dir)";
std::cout << '\n';
}
return(EXIT_SUCCESS);
}
This code compiles and runs fine... But my adaptation, on the other hand, is crashing at runtime. Here is a snippit of my code.
Code:
void mSearchDirs(string pathNm){
path fullPath = system_complete(path(pathNm, native));
int i = 0;
directory_iterator end;
string tempString;
for (directory_iterator it(fullPath);it != end; ++it) {
tempString = it->leaf( );
if (is_directory(*it)){
g_dirsFound[i] = tempString;
i++;
}
}
}
I'm having trouble finding out why this is crashing, and how to get it running. A couple things I know are, is_directory() works with literals, for instance, if I were to say is_directory("C:\\WINDOWS"); it would return true. But if I pass it a string with that value, wont go. Also, it'll work with the boost type path. So, for example, if I pass it is_directory(fullPath); it wont crash, but will display all paths, naturally.
*it is the path iterator, so, sending the arg *it would seem to be sending a single element of fullPath, but it just doesent work... :S
Any ideas?
boost is using exceptions for error handling.
add something like:
try
{
// your code
}
catch(boost::filesystem::filesystem_error& x)
{
// debug output
cout << x.what() << endl;
}
add something like:
try
{
// your code
}
catch(boost::filesystem::filesystem_error& x)
{
// debug output
cout << x.what() << endl;
}