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,并且是文件夹,最后执行了删除操作并执行成功。

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Powered by Discuz! X3.5  © 2001-2013 Comsenz Inc.