Open方法打开只有完整路径C++的文件

Joh*_*hnG 2 c++ file-io xcode ifstream

如果我写完整路径(full-path/roots.txt),文件将打开.如果我只写文件名(roots.txt),文件无法打开

然而,roots.txt与main.cpp位于同一个文件夹中.我应该在XCode上查看任何设置吗?

这是代码:

    string line;
ifstream infile;
infile.clear();
// infile.open("roots.txt");
infile.open("/Users/programming/C++/roots/roots.txt");
if (infile.fail()) cout << "could not open the file: " << strerror(errno);
getline(infile, line);
cout << line;
Run Code Online (Sandbox Code Playgroud)

Kaj*_*aje 9

默认情况下,Xcode工作目录就像是~/Library/Developer/Xcode/DerivedData/project-/Build/Products/Debug.这样,如果您尝试使用相对于项目目录或任何其他目录的相对路径,则应在Xcode中手动配置工作目录.

您可以通过以下方式执行此操作:产品 - >方案 - >编辑方案 - >选项选项卡,勾选使用自定义工作目录复选框并显示您的路径.