删除编程C或C非空目录++ [英] Removing a non empty directory programmatically in C or C++

查看:147
本文介绍了删除编程C或C非空目录++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  删除文件夹项

如何删除C或C ++一个非空目录?有什么功能?命令rmdir只删除空目录。请提供一种方式,而无需使用任何外部库。

How to delete a non empty directory in C or C++? Is there any function? rmdir only deletes empty directory. Please provide a way without using any external library.

也告诉我如何删除C或C ++中的文件?

Also tell me how to delete a file in C or C++?

推荐答案

您想编写一个函数(递归函数是最简单的,但可以很容易用完深目录栈空间),将枚举目录的孩子。如果你发现孩子这是一个目录,你递归上。否则,你里面删除文件。当您完成,该目录是空的,您可以通过系统调用删除它。

You want to write a function (a recursive function is easiest, but can easily run out of stack space on deep directories) that will enumerate the children of a directory. If you find a child that is a directory, you recurse on that. Otherwise, you delete the files inside. When you are done, the directory is empty and you can remove it via the syscall.

要列举的目录在Unix上,你可以使用执行opendir READDIR closedir 。要删除您使用命令rmdir()上一个空目录(即你的函数结束后,删除子女)和取消链接()上的文件。请注意,在许多系统上的 d_type 成员结构的dirent 不支持;在这些平台上,你将不得不使用 STAT() S_ISDIR(stat.st_mode)来确定给定路径是一个目录。

To enumerate directories on Unix, you can use opendir, readdir, and closedir. To remove you use rmdir() on an empty directory (i.e. at the end of your function, after deleting the children) and unlink() on a file. Note that on many systems the d_type member in struct dirent is not supported; on these platforms, you will have to use stat() and S_ISDIR(stat.st_mode) to determine if a given path is a directory.

在Windows中,您将使用用FindFirstFile() / FindNextFile()来列举, RemoveDirectory()的空目录,而的DeleteFile()删除文件。

On Windows, you will use FindFirstFile()/FindNextFile() to enumerate, RemoveDirectory() on empty directories, and DeleteFile() to remove files.

下面是一个例子,可能在Unix(完全未经测试)工作:

Here's an example that might work on Unix (completely untested):

int remove_directory(const char *path)
{
   DIR *d = opendir(path);
   size_t path_len = strlen(path);
   int r = -1;

   if (d)
   {
      struct dirent *p;

      r = 0;

      while (!r && (p=readdir(d)))
      {
          int r2 = -1;
          char *buf;
          size_t len;

          /* Skip the names "." and ".." as we don't want to recurse on them. */
          if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
          {
             continue;
          }

          len = path_len + strlen(p->d_name) + 2; 
          buf = malloc(len);

          if (buf)
          {
             struct stat statbuf;

             snprintf(buf, len, "%s/%s", path, p->d_name);

             if (!stat(buf, &statbuf))
             {
                if (S_ISDIR(statbuf.st_mode))
                {
                   r2 = remove_directory(buf);
                }
                else
                {
                   r2 = unlink(buf);
                }
             }

             free(buf);
          }

          r = r2;
      }

      closedir(d);
   }

   if (!r)
   {
      r = rmdir(path);
   }

   return r;
}

这篇关于删除编程C或C非空目录++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆