打开方法打开仅具有完整路径的文件C ++ [英] Open method opens files with full path only C++
问题描述
如果我写完整路径(full-path / roots.txt),文件将打开。
如果我只写文件名(roots.txt),文件无法打开
File opens if I write the full path (full-path/roots.txt). File fails to open if I write the filename only (roots.txt)
然而,roots.txt与main.cpp 。
有什么设置我应该检查XCode?
And yet, roots.txt is in the same folder as the main.cpp. Is there any settings I should check on 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;
推荐答案
一个绝对路径和失败,只有文件名,你的相对路径可能是不正确的。确保 roots.txt
放置在当前工作目录中。查看 unistd.h
中声明的 getcwd
函数。
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
.
这篇关于打开方法打开仅具有完整路径的文件C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!