首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在C++中删除文件夹?

如何在C++中删除文件夹?
EN

Stack Overflow用户
提问于 2009-04-09 15:26:23
回答 17查看 126.5K关注 0票数 54

如何使用C++删除文件夹?

如果不存在跨平台的方法,那么如何为最流行的OSes - Windows,Linux,Mac,iOS,Android做跨平台呢?POSIX解决方案能解决所有这些问题吗?

EN

回答 17

Stack Overflow用户

发布于 2009-04-09 15:36:35

我强烈建议使用Boost.FileSystem。

http://www.boost.org/doc/libs/1_38_0/libs/filesystem/doc/index.htm

在您的情况下,这将是

boost::filesystem::remove_all(yourPath)

票数 63
EN

Stack Overflow用户

发布于 2017-04-13 20:58:31

在C++17中,您可以使用std::filesystem,在C++14中std::experimental::filesystem已经可用。两者都允许使用filesystem::remove()

C++17:

代码语言:javascript
运行
复制
#include <filesystem>
std::filesystem::remove("myEmptyDirectoryOrFile"); // Deletes empty directories or single files.
std::filesystem::remove_all("myDirectory"); // Deletes one or more files recursively.

C++14:

代码语言:javascript
运行
复制
#include <experimental/filesystem>
std::experimental::filesystem::remove("myDirectory");

注1:这些函数在出现错误时抛出filesystem_error。如果您希望避免捕获异常,请使用重载的变体,并将std::error_code作为第二个参数。例如。

代码语言:javascript
运行
复制
std::error_code errorCode;
if (!std::filesystem::remove("myEmptyDirectoryOrFile", errorCode)) {
    std::cout << errorCode.message() << std::endl;
}

注2:从不同的编码隐式地转换为std::filesystem::path,因此您可以将字符串传递给filesystem::remove()

票数 52
EN

Stack Overflow用户

发布于 2012-05-31 23:03:42

在Windows (VisualC++)中删除文件夹(sub_folders和文件)不使用外壳API,这是最好的工作示例:

代码语言:javascript
运行
复制
#include <string>
#include <iostream>

#include <windows.h>
#include <conio.h>


int DeleteDirectory(const std::string &refcstrRootDirectory,
                    bool              bDeleteSubdirectories = true)
{
  bool            bSubdirectory = false;       // Flag, indicating whether
                                               // subdirectories have been found
  HANDLE          hFile;                       // Handle to directory
  std::string     strFilePath;                 // Filepath
  std::string     strPattern;                  // Pattern
  WIN32_FIND_DATA FileInformation;             // File information


  strPattern = refcstrRootDirectory + "\\*.*";
  hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);
  if(hFile != INVALID_HANDLE_VALUE)
  {
    do
    {
      if(FileInformation.cFileName[0] != '.')
      {
        strFilePath.erase();
        strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;

        if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
          if(bDeleteSubdirectories)
          {
            // Delete subdirectory
            int iRC = DeleteDirectory(strFilePath, bDeleteSubdirectories);
            if(iRC)
              return iRC;
          }
          else
            bSubdirectory = true;
        }
        else
        {
          // Set file attributes
          if(::SetFileAttributes(strFilePath.c_str(),
                                 FILE_ATTRIBUTE_NORMAL) == FALSE)
            return ::GetLastError();

          // Delete file
          if(::DeleteFile(strFilePath.c_str()) == FALSE)
            return ::GetLastError();
        }
      }
    } while(::FindNextFile(hFile, &FileInformation) == TRUE);

    // Close handle
    ::FindClose(hFile);

    DWORD dwError = ::GetLastError();
    if(dwError != ERROR_NO_MORE_FILES)
      return dwError;
    else
    {
      if(!bSubdirectory)
      {
        // Set directory attributes
        if(::SetFileAttributes(refcstrRootDirectory.c_str(),
                               FILE_ATTRIBUTE_NORMAL) == FALSE)
          return ::GetLastError();

        // Delete directory
        if(::RemoveDirectory(refcstrRootDirectory.c_str()) == FALSE)
          return ::GetLastError();
      }
    }
  }

  return 0;
}


int main()
{
  int         iRC                  = 0;
  std::string strDirectoryToDelete = "c:\\mydir";


  // Delete 'c:\mydir' without deleting the subdirectories
  iRC = DeleteDirectory(strDirectoryToDelete, false);
  if(iRC)
  {
    std::cout << "Error " << iRC << std::endl;
    return -1;
  }

  // Delete 'c:\mydir' and its subdirectories
  iRC = DeleteDirectory(strDirectoryToDelete);
  if(iRC)
  {
    std::cout << "Error " << iRC << std::endl;
    return -1;
  }

  // Wait for keystroke
  _getch();

  return 0;
}

来源:http://www.codeguru.com/forum/showthread.php?t=239271

票数 23
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/734717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档