侯芳芳 發表於 2025-4-27 14:04:00

目录操作相关函数

<h1 id="mkdir函数">mkdir函数</h1>
<p>此函数用于创建一个目录</p>
<pre><code>//头文件
#include &lt;sys/stat.h&gt;
#include &lt;sys/types.h&gt;

int mkdir(const char *pathname,mode_t mode);
//pathname:目录的路径
//mode:目录的权限
//ret:成功返回0,失败返回-1
</code></pre>
<h1 id="rmdir函数">rmdir函数</h1>
<p>此函数用于删除一个目录</p>
<pre><code>//头文件
#include &lt;unistd.h&gt;

int rmdir(const char *pathname);
//pathname:目录的路径
//ret:成功返回0,失败返回-1
//notice:要删除的目录必须是空的
</code></pre>
<h1 id="opendir函数">opendir函数</h1>
<p>此函数用于打开目录,打开目录并不意味着进入目录</p>
<pre><code>//头文件
#include &lt;sys/types.h&gt;
#include &lt;dirent.h&gt;

DIR *opendir(const char *name);
//name:目录的路径
//ret:成功返回指向目录入口的指针,失败返回NULL
</code></pre>
<h1 id="chdir函数">chdir函数</h1>
<p>此函数用于改变工作目录</p>
<pre><code>//头文件
#include &lt;unistd.h&gt;

int chdir(const char *path);
//path:要进入的目录的路径
//ret:成功返回0,失败返回-1
</code></pre>
<h1 id="readdir函数">readdir函数</h1>
<p>此函数用于读取目录</p>
<pre><code>//头文件
#include &lt;dirent.h&gt;

struct dirent *readdir(DIR *dirp);
//dirp:指的是待读取目录的目录指针
//ret:成功返回一个指向该目录中下一个目录项的指针,失败返回NULL
</code></pre>
<p>调用完此函数,dirp会指向下一个文件,如果到达末尾会返回NULL<br>
<img src="https://img2024.cnblogs.com/blog/3626997/202504/3626997-20250427135846362-1702069727.png" alt="image" loading="lazy"></p>
<p><img src="https://img2024.cnblogs.com/blog/3626997/202504/3626997-20250427135911472-2138738239.png" alt="image" loading="lazy"></p>
<h1 id="stat函数">stat函数</h1>
<p>此函数用于获取文件的信息</p>
<pre><code>//头文件
#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;unistd.h&gt;

int stat(const char *pathname, struct stat *buf);
//pathname:待读取文件的路径
//buf:获取的文件信息结构体的地址
</code></pre>
<p><img src="https://img2024.cnblogs.com/blog/3626997/202504/3626997-20250427140324695-1978467982.png" alt="image" loading="lazy"></p>
<p><img src="https://img2024.cnblogs.com/blog/3626997/202504/3626997-20250427140418863-516293676.png" alt="image" loading="lazy"></p><br><br>
来源:https://www.cnblogs.com/lradian/p/18849336
頁: [1]
查看完整版本: 目录操作相关函数