C ++通过相对路径在目录外部打开文件不起作用

鲁迪·高伯特

在工作目录之外的C ++中打开文件使用本文中代码的格式,我尝试使用相对路径打开文件。

std::ifstream rules("/static/rules.txt");

在将“ rules.txt”放入“静态”文件夹之前,我能够做到这一点:

std::ifstream rules("rules.txt");

没有错误。

阿米特(Amit Vujic)
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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章