Boost文件系统总是在Windows上返回false

时间:2014-03-30 18:24:17

标签: c++ windows boost-filesystem

Everything编译文件。但是,当我逐步浏览Visual Studio 12中的代码时,Boost检查文件夹是否实际存在的行会跳转else语句。该文件夹位于正确的路径(与.exe相同的路径),如果我没记错,相同的代码在Ubuntu中工作没有问题。我已经阅读了Boost文档,但我无法真正看到我做错了什么。

的main.cpp

#include "game.hpp"
#include "texturemanager.hpp"
#include "spritemanager.hpp"
#include "imagemanager.hpp"

int main()
{
  //Texture manager to hold all textures for the game. 
  TextureManager::GetInstance().LoadContent("Data/Images/Sprites");

  //Sprite manager to hold all backgrounds used in the game
  SpriteManager::GetInstance().LoadContent("Data/Images/Backgrounds");

  //Create the game. Takes in the parameters of the size of the screen
  Game game(640, 480);
  game.Run();
}

texturemanager.hpp

#ifndef TEXTUREMANAGER_HPP
#define TEXTUREMANAGER_HPP

#include <SFML/Graphics.hpp>
#include <map>
#include <string>
#include <memory>
#include <assert.h>
#include <boost/filesystem.hpp>

class TextureManager
{
public:
  typedef std::map<int, std::unique_ptr<sf::Texture>> resourceMap;

  //For creating a singleton class
  static TextureManager &GetInstance();

  //Load files from the folder loaded from the settings file
  void LoadContent(const std::string&);
  //Load files from folder and with a specific file name
  //To be added later to reduce memory usage
  //void LoadContent(const std::string&);

  //Get the texture
  const sf::Texture& Get(int) const;
  sf::Texture& Get(int);
protected:
private:
  //These are needed to create a singleton class
  TextureManager(){}
  TextureManager(TextureManager const&){}
  void operator=(TextureManager const&){}

  //Add a file to the resource map
  void Insert(boost::filesystem::path);

  //Hold the textures in memory
  resourceMap mTextureMap;
};

#endif

texturemanager.cpp

#include "texturemanager.hpp"

TextureManager& TextureManager::GetInstance()
{
  static TextureManager instance;
  return instance;
}

void TextureManager::LoadContent(const std::string& folder)
{
  //Create a temp vector for the file entries to go to
  std::vector<boost::filesystem::directory_entry> files;

  //Create the directory where all sprites will be stored
  boost::filesystem::path spriteDir(folder);

  spriteDir.make_preferred();

  //Check to see if the directory exists
  if(boost::filesystem::exists(spriteDir))
  {
    //Copy all of the entries into the directory vector
    copy(boost::filesystem::directory_iterator(spriteDir),
         boost::filesystem::directory_iterator(),
         back_inserter(files));
  }
  else
  {
    std::cout << folder + " Does not exist" << std::endl;
  }

  //Check to see if anything was stored
  if(files.size() > 0)
  {
    //Loop through each of the files
    for(unsigned int i = 0; i < files.size(); i++)
    {
      if(files[i].path().extension() == ".png")
      {
        //Convert the file to a string and push into vector
        Insert(files[i].path());
      }
    }
    //Empty the vector for reuse
    files.clear();
  }
}

void TextureManager::Insert(boost::filesystem::path tempFile)
{
  //Create and load resource
  std::unique_ptr<sf::Texture> tempTexture(new sf::Texture());

  if(!tempTexture->loadFromFile(tempFile.string()))
    throw std::runtime_error("ResourceHolder::load - Failed to load " + tempFile.string());


  std::string srtID = tempFile.stem().string();

  int id = std::atoi(srtID.c_str());

  //Insert and check success
  auto inserted = mTextureMap.insert(std::make_pair(id, std::move(tempTexture)));
  assert(inserted.second);
}

1 个答案:

答案 0 :(得分:2)

相对路径不是相对于可执行文件本身,而是相对于工作目录。因此,如果双击exe,工作目录将正确设置为与可执行文件相同的位置,但如果在Visual Studio中启动它,它将使用设置中设置的工作目录。您可以在Debugging->Working Directory下的项目设置中找到该选项:

enter image description here

与手头的问题无关,我强烈建议您不要使用单例模式作为纹理管理器,而应该选择一种不需要从任何地方访问管理器的设计,尽可能减少依赖关系,因此,通过const引用传递一个好的/干净的方式去。