花莲县网站建设_网站建设公司_外包开发_seo优化
2026/1/16 18:37:46 网站建设 项目流程

rmdir用于删除空目录。

1.头文件

#include <unistd.h>

2.函数原型

int rmdir(const char *pathname);

3.参数

pathname:要删除的目录的路径。这个目录必须是空的,否则 rmdir 会失败。

4.返回值

成功返回0,失败返回-1,并设置errno。

5.示例:(使用rmdir删除空目录

#include <stdio.h>

#include <sys/stat.h>

#include <unistd.h>

int main(){

const char *dir_path = "directory";

struct stat statbuf;

printf("File name:%s\n", dir_path);

if (stat(dir_path, &statbuf) == -1) {

perror("stat");

return 1;

}

if (S_ISDIR(statbuf.st_mode)) {

printf("It's the directory: YES\n");

if (rmdir(dir_path) == -1) {

perror("rmdir");

return 1;

}

printf("Directory '%s' deleted successfully.\n", dir_path);

} else {

printf("It's the directory: NO\n");

}

return 0;

}

自行创建directory文件夹,并执行rmdir_test命令:

$ ls

directory rmdir_test rmdir_test.c

$ ./rmdir_test

File name:directory

It's the directory: YES

Directory 'directory' deleted successfully.

$ ls

rmdir_test rmdir_test.c

可以在执行信息中看到,要删除的文件为directory,并且是文件夹,最后执行了删除操作并执行成功。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询