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:%sn", dir_path); if (stat(dir_path, &statbuf) == -1) { perror("stat"); return 1; } if (S_ISDIR(statbuf.st_mode)) { printf("It's the directory: YESn"); if (rmdir(dir_path) == -1) { perror("rmdir"); return 1; } printf("Directory '%s' deleted successfully.n", dir_path); } else { printf("It's the directory: NOn"); } 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,并且是文件夹,最后执行了删除操作并执行成功。
193