gpt4 book ai didi

c++ - 打开方法仅使用完整路径打开文件 C++

转载 作者:太空狗 更新时间:2023-10-29 23:23:34 26 4
gpt4 key购买 nike

如果我写入完整路径 (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;

最佳答案

默认情况下,Xcode 工作目录类似于 ~/Library/Developer/Xcode/DerivedData/project-/Build/Products/Debug。 因此,如果您尝试使用相对路径尊重您的项目目录或任何其他目录,您应该在 Xcode 中手动配置您的工作目录。

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

关于c++ - 打开方法仅使用完整路径打开文件 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4796943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com