C++ 我能';加载数据文件时,总是无法打开它

C++ 我能';加载数据文件时,总是无法打开它,c++,raytracing,C++,Raytracing,我实际上在写一个光线跟踪器,我有一个函数可以加载.off。我正在使用xcode,在同一个文件夹中有my.h.cpp和my.off。这是我功能的开始: void Mesh::loadOFF (const std::string & filename) { clear (); ifstream input (filename.c_str ()); if (!input) throw Exception ("Failing opening the file.");

我实际上在写一个光线跟踪器,我有一个函数可以加载.off。我正在使用xcode,在同一个文件夹中有my.h.cpp和my.off。这是我功能的开始:

void Mesh::loadOFF (const std::string & filename) {
   clear ();
   ifstream input (filename.c_str ());
   if (!input)
      throw Exception ("Failing opening the file.");
电话是:

Mesh sphereMesh;
sphereMesh.loadOFF ("sphere.off");

我不明白如果我的.off和文件在同一个文件夹中,打开总是失败的原因。有人能帮我吗?

Xcode程序显然是在一个有点奇怪的位置构建的。似乎它们通常位于

~/Library/Developer/Xcode/DerivedData/<project-specific-directory>/Products/<config>
似乎一般建议在尝试打开现有文件时不要依赖相对目录。如果要打开相对于当前目录的文件,可能应该使用程序中的
chdir()
系统调用相应地设置目录:

int main() {
    chdir("/path-to-the-directory-with-your-file/");
    // ...
}

确保将工作目录设置为正确的位置。例如,您可能希望从命令行运行代码:
cd
到正确的目录,并使用
/您的程序名在这里
。可执行文件是否与
h
cpp
文件在同一文件夹中创建?您好,感谢您的回复,但是我没有./your program name在folderI中的这一部分,实际上我对xcode是新的,所以我不知道可执行文件在哪里:s
int main() {
    chdir("/path-to-the-directory-with-your-file/");
    // ...
}