我正在使用Visual Studio 2011 C ++和boost v1.52.0刚添加的方法来搜索子目录中的文件并开始在程序关闭时出现此错误:
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("");
}
我以这种方式使用它:
std::string imageFullPath = FileSystem::GetFullPath("image.png");
这段代码有什么问题?感谢。
答案 0 :(得分:0)
首先,我没有提到我使用的是SDL(Simple DirectMedia Layer)库。使用#pragma comment(lib,...)链接这些库,这会导致应用程序关闭时出错。我在项目属性中定义了它们 - >链接器 - >输入 - >其他依赖关系部分蚂蚁问题已经解决。
其次GetFullPath方法错误。这是正确的:
const std::string FileSystem::GetFullPath(std::string fileName, std::string searchInPath)
{
std::string result;
if(searchInPath.empty()) {
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()))
{
result = GetFullPath(fileName, dir_iter->path().string());
if(!result.empty())
{
break;
}
}
else if (fs::is_regular_file(dir_iter->status()))
{
if(dir_iter->path().filename() == fs::path(fileName).filename())
{
result = dir_iter->path().string();
if(!result.empty())
{
break;
}
}
}
}
return result;
}