因為rmdir只能刪除空文件夾,而我實現的功能相當于 rm -rf path...
實現的功能:
? 遞歸刪除指定文件夾的所有文件
程序說明:
1.?my_rmdir(): 即為遞歸刪除動作的自定義函數。
2.?opendir(), readdir(), closedir(): 讀取目錄信息。
3.?rmdir(): 刪除空的文件夾; remove(): 刪除文件或文件夾。
程序編譯運行(見下圖):
程序源碼:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#define PATH_SIZE 4094
void my_rmdir(const char * path);
/* rm -rf path: ./a.out path */
int main(int argc, char const *argv[])
{
if (argc != 2)
{
fprintf(stdout, "argument error!\n");
return 1;
}
my_rmdir(argv[1]);
return 0;
}
void my_rmdir(const char * path)
{
DIR *dirp;
dirp = opendir(path);
if (NULL == dirp)
{
perror(path);
return;
}
struct dirent *entry;
int ret;
while (1)
{
entry = readdir(dirp);
if (NULL == entry)
{
break;
}
// skip . & ..
if (0 == strcmp(".", entry->d_name) || 0 == strcmp("..", entry->d_name))
{
continue;
}
char buf[PATH_SIZE];
snprintf(buf, PATH_SIZE, "%s/%s", path, entry->d_name);
ret = remove(buf);
if (-1 == ret)
{
if (ENOTEMPTY == errno)
{
my_rmdir(buf);
continue;
}
perror(buf);
return;
}
fprintf(stdout, "rm file: %s\n", buf);
}
closedir(dirp);
ret = rmdir(path);
if (-1 == ret)
{
perror(path);
return;
}
fprintf(stdout, "rm dir: %s\n", path);
}
```
---------------------
原文:https://blog.csdn.net/a_ran/article/details/25250583