Closed. This question is not reproducible or was caused by typos。它当前不接受答案。












想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

在4个月前关闭。



Improve this question




Opening a file in C++ outside of the working directory
使用本文中代码的格式,我尝试使用相对路径打开文件。
std::ifstream rules("/static/rules.txt");
在将“rules.txt”放入“静态”文件夹之前,我能够做到这一点:
std::ifstream rules("rules.txt");
没有错误。

最佳答案

Let say that your working directory is "/opt/usr1"
您必须考虑以下几个要素:
1) Path to files

"/static/rules.txt"    <---Absolute path from root "/" folder,
                           meaning that your code will look for file "/static/rules.txt"
"./static/rules.txt"   <---Relative path from working directory meaning that your
                           code will look for file "/opt/usr1/static/rules.txt"
"rules.txt"            <---Relative path from working directory meaning that
                           code will look for "/opt/usr1/rules.txt"


2) Ownership on folders and files, so check out what user
   is running process and user privileges upon folders and files.

关于c++ - C++通过相对路径在目录外部打开文件不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63383272/

10-11 23:07
查看更多