最近我一直在开发一个小的 OpenGL 游戏。其中的所有内容在调试版本中运行良好,但是当我构建版本时,我得到了一个奇怪的访问冲突异常。
我搜索了代码,似乎在我尝试打开文件时出现了问题。这是我认为问题出在的函数:
#define LOCAL_FILE_DIR "data\\"
#define GLOBAL_FILE_DIR "..\\data\\"
std::string FindFile(const std::string &baseName)
{
std::string fileName = LOCAL_FILE_DIR + baseName;
std::ifstream testFile(fileName.c_str()); // The code breaks here
if(testFile.is_open())
return fileName;
fileName = GLOBAL_FILE_DIR + baseName;
testFile.open(fileName.c_str());
if(testFile.is_open())
return fileName;
throw std::runtime_error("Could not find the file " + baseName);
}
此代码与加载 GLSL 着色器相关联。一个函数获取着色器的文件名,然后将其传递给 FindFile 以查找所需的文件。