I’m using Visual Studio 2011 C++ with boost v1.52.0 Just added method to search for file in sub directories and started to get this error on program close:
Unhandled exception at 0x7733E3BE (ntdll.dll) in Platformer.exe: 0xC0000005: Access violation reading location 0x437FEF0C.
FileSystem.h:
#ifndef FILESYSTEM_H
#define FILESYSTEM_H
#include "boost/filesystem.hpp"
namespace fs=boost::filesystem;
class FileSystem
{
public:
static const std::string GetExePath();
static const std::string GetFullPath(std::string fileName, std::string searchInPath = "");
};
#endif
FileSystem.cpp
#include "FileSystem.h"
const std::string FileSystem::GetExePath()
{
fs::path path = fs::current_path();
return path.string();
}
const std::string FileSystem::GetFullPath(std::string fileName, std::string searchInPath)
{
if(searchInPath == "")
{
searchInPath = GetExePath();
}
fs::directory_iterator end_iter;
for( fs::directory_iterator dir_iter(searchInPath); dir_iter!=end_iter; ++dir_iter)
{
if(fs::is_directory(dir_iter->status()))
{
return GetFullPath(fileName, dir_iter->path().string());
}
else if (fs::is_regular_file(dir_iter->status()))
{
if(dir_iter->path().filename() == fs::path(fileName).filename())
{
return dir_iter->path().string();
}
}
}
return std::string("");
}
And I use it in that way:
std::string imageFullPath = FileSystem::GetFullPath("image.png");
What is wrong with this code? Thanks.
Firstly I didn’t mentioned that I was using SDL (Simple DirectMedia Layer) libraries. These libraries were linked using #pragma comment (lib, …) That causes errors on the application closing. I defined them in Project Properties -> Linker -> Input -> Additional Dependencies section ant the problem was solved.
Secondly GetFullPath method was wrong. Here is the correct one: