Boost filesystem runtime error!

I can not find the cause of this error, the line I think the error occurs on is:
 
  for (boost::filesystem::recursive_directory_iterator iterator(directory); iterator != end_iterator; ++iterator)


Nothing past this point will run, I get this error:
Exception thrown at 0x75ACDAE8 in Game.exe: Microsoft C++ exception: boost::filesystem::filesystem_error at memory location 0x008FF8E0.

Unhandled exception at 0x75ACDAE8 in Game.exe: Microsoft C++ exception: boost::filesystem::filesystem_error at memory location 0x008FF8E0.

I would post more relevant code, but I am totally lost with this error.
It told you that you didn't catch the exception. Catch it, and print its what(), it will tell you what the problem was. The most common problem is that directory doesn't exist:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <boost/filesystem.hpp>
#include <iostream>
namespace fs = boost::filesystem;
int main() try
{
    fs::path directory = "C:This\\Doesn't\\Exist";
    for(fs::path p : fs::recursive_directory_iterator(directory)) // C++11 version of yours
    {
        std::cout << p << '\n';
    }
} catch(const fs::filesystem_error& e) {
    std::cout << e.what() << '\n';
}

demo: http://rextester.com/VIHF85465
(demo that doesn't throw: http://rextester.com/FHP63943 )
Last edited on
Thanks!
After I woke up I fixed the error(Was only using one backslash); but now I am getting a read access violation in xstring:

Here is the full code, it is a function that scans a directory and finds files of certain extensions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "Filesystem.h"
#include <vector>
#include <string>
#include <boost/filesystem.hpp>
#include <algorithm>
#include <iostream>

std::vector<std::string> Filesystem::scan(std::string directory, std::vector<std::string> filters)
{

	//Stores index of directory
	std::vector<std::string> index;

	//This is the end iterator
	boost::filesystem::recursive_directory_iterator end_iterator;

	//Cycle through the directory(and subdirectories)
	
	try
	{
		for (boost::filesystem::recursive_directory_iterator iterator(directory); iterator != end_iterator; ++iterator)
		{
			//push back name of current file to vector
			std::string current_directory = iterator->path().string();
			std::cout << current_directory << std::endl;
			//add it to the index(but not if filtered out)
			for (const auto& filter : filters)
			{
				//If there is no filter
				if (filter == "*")
				{
					//Add to index
					index.push_back(current_directory);
					break;
				}
				//if this file has an acceptible extension(filtered)
				else if (iterator->path().extension() == filter)
				{
					std::cout << current_directory << std::endl;
					index.push_back(current_directory);
				}
			}
		}
	}
	catch (const boost::filesystem::filesystem_error& e)
	{
		std::cout << e.what() << '\n';
	}
	return index;
}


Here is where I am calling it

1
2
3
4
Filesystem::scan("./resources/music" , { "ogg", "wav", "flac", "aiff", "au", "raw",
											 "paf", "svx", "nist", "voc", "ircam", "w64",
											 "mat4", "mat5", "pvf", "htk", "sds", "avr",
											 "sd2", "caf", "wve", "mpc2k", "rf64" });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace fs = boost::filesystem ;

static bool filtered_in( const fs::path& path, const std::vector<std::string> filters )
{
    if( fs::is_regular_file(path) )
       for( const auto& ext : filters ) if( path.extension() == ext ) return true ;

    return false ;
}

std::vector<std::string> scan( const fs::path& path, std::vector<std::string> filters )
{
    std::vector<std::string> result ;
    if( !fs::is_directory(path) ) return result ;

    const bool no_filter = std::find( std::begin(filters), std::end(filters), "*" ) != std::end(filters) ;
    if( !no_filter ) for( auto& ext : filters ) if( !ext.empty() && ext.front() != '.' ) ext = '.' + ext ;

    using iter_type = fs::recursive_directory_iterator ;
    for( iter_type iter(path) ; iter != iter_type{} ; ++iter )
    {
        if( no_filter || filtered_in( *iter, filters ) ) result.push_back( iter->path().string() ) ;
    }

    return result ;
}

http://coliru.stacked-crooked.com/a/9a74acb2a4f48d1f
http://rextester.com/PYJE88307
Topic archived. No new replies allowed.