[英]How can I delete a directory and all files in it with C++?
如何刪除目錄中的所有文件? 我使用了rmdir以及互聯網上建議的其他方法,但沒有人幫助我:這是其中之一:(我想在當前工作目錄中刪除目錄tmp)
removeDir()
{
char currentPath[_MAX_PATH];
GetCurrentPath(currentPath);
std::string tmp(currentPath);
string path = tmp + "\\temp";
std::string command = "del ";
std::string Path = path + "1.txt";
cout << Path << endl;
system(command.append(Path).c_str());
}
GetCurrentPath(char* buffer)
{
getcwd(buffer, _MAX_PATH);
}
您應該查看Boost Filesystem庫 ,該庫提供了許多功能,可以使這種事情變得容易得多。 鏈接頁面上的示例代碼執行的操作與您要完成的操作非常相似(它以遞歸方式搜索目錄,而不是以遞歸方式刪除內容)。
如果您不想使用Boost,可以執行此操作
rm -r "folder name"
http://www.cplusplus.com/reference/clibrary/cstdio/remove/
int remove ( const char * filename );
#include <stdio.h>
int main ()
{
if( remove( "myfile.txt" ) != 0 )
perror( "Error deleting file" );
else
puts( "File successfully deleted" );
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.