简体   繁体   English

如何使用fopen打开当前目录中的文件夹内的文件

[英]How to open file inside folder in current directory using fopen

I want to open a file that is inside a folder in the current working directory like so: 我想打开当前工作目录中文件夹内的文件,如下所示:

fopen("/folder/file.txt","r");

I'm unable to do so in this way, i get a "No such file or directory" error. 我无法以这种方式这样做,我得到一个“没有这样的文件或目录”错误。

How can i do this properly? 我该怎么做呢?

Thank you in advance for any help. 预先感谢您的任何帮助。

You have to mention that is a current directory. 你必须提到这是一个当前目录。 Try this, 尝试这个,

fopen("./folder/file.txt","r");

Or 要么

fopen("folder/file.txt","r");

If you mentioning like this /folder/file.txt it will search the directory from the root directory. 如果你提到这样的/folder/file.txt ,它将从根目录中搜索目录。 So this is the reason for getting the error. 所以这就是获得错误的原因。

Try: 尝试:

fopen("./folder/file.txt","r"); /* dot means the directory itself */

or 要么

fopen("folder/file.txt","r"); /* without the first backslash */

It seems that if i remove the first backslash it works. 似乎如果我删除第一个反斜杠它的工作原理。

Like so: 像这样:

fopen("folder/file.txt","r"); 的fopen( “文件夹/ file.txt的”, “R”);

weird. 奇怪的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM