[英]How can I delete a directory and all files in it with C++?
How can I delete all the files in the directory? 如何删除目录中的所有文件? I've used rmdir and other methods suggested in the internet but no one helped me: this is one of them: (I want to remove directory tmp in the current work directory) 我使用了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);
}
You should look into the Boost Filesystem Library , which provides a number of features that make this sort of thing a lot easier. 您应该查看Boost Filesystem库 ,该库提供了许多功能,可以使这种事情变得容易得多。 The example code on the linked page does something very similar to what you want to accomplish (it searches a directory recursively instead of deleting contents recursively). 链接页面上的示例代码执行的操作与您要完成的操作非常相似(它以递归方式搜索目录,而不是以递归方式删除内容)。
如果您不想使用Boost,可以执行此操作
rm -r "folder name"
http://www.cplusplus.com/reference/clibrary/cstdio/remove/ 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.