xcode Open 方法仅使用完整路径打开文件 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4796943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open method opens files with full path only C++
提问by JohnG
File opens if I write the full path (full-path/roots.txt). File fails to open if I write the filename only (roots.txt)
如果我写完整路径 (full-path/roots.txt),文件会打开。如果我只写文件名(roots.txt),文件将无法打开
And yet, roots.txt is in the same folder as the main.cpp. Is there any settings I should check on XCode?
然而,roots.txt 与 main.cpp 位于同一文件夹中。我应该检查 XCode 上的任何设置吗?
Here's the code:
这是代码:
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;
回答by Kaje
By default, Xcode working directory is something like ~/Library/Developer/Xcode/DerivedData/project-/Build/Products/Debug.
So,if you are trying to use a relative path with respect your project directory or any other, you should manually configure your working directory in Xcode.
默认情况下,Xcode 工作目录类似于~/Library/Developer/Xcode/DerivedData/project-/Build/Products/Debug.
So,如果您尝试使用相对于您的项目目录或任何其他目录的相对路径,您应该在 Xcode 中手动配置您的工作目录。
You can do this by: Product -> Scheme -> Edit Scheme -> options tab, tick the use custom working directory checkbox and show your path.
您可以通过以下方式执行此操作:Product -> Scheme -> Edit Scheme -> options tab,勾选使用自定义工作目录复选框并显示您的路径。
回答by James
If it works when you attempt to open a file with an absolute path and fails with just the filename, your relative path is likely incorrect. Ensure roots.txt
is placed in the current working directory. Look into the getcwd
function declared in unistd.h
.
如果它在您尝试使用绝对路径打开文件时有效并且仅使用文件名失败,则您的相对路径可能不正确。确保roots.txt
放置在当前工作目录中。查看中getcwd
声明的函数unistd.h
。
回答by molbdnilo
To change the working directory when you're running from inside XCode: select "Edit Active Executable" in your "Project" menu.
要在从 XCode 内部运行时更改工作目录:在“项目”菜单中选择“编辑活动可执行文件”。
You can adjust your working directory settings at the bottom of the "General" section.
您可以在“常规”部分的底部调整您的工作目录设置。
回答by Billy ONeal
Assuming you're running file from within XCode, the working directory is unlikely to be the same directory where your .cpp file is located. Check what your current working directory is what you think it is. (you should be able to obtain it using a getcwd
call)
假设您在 XCode 中运行文件,工作目录不太可能与 .cpp 文件所在的目录相同。检查您当前的工作目录是您认为的内容。(您应该可以通过getcwd
电话获得它)