2011-09-17 164 views
0

如何刪除目錄中的所有文件?我用命令rmdir並在互聯網上提出其他方法,但是沒有一個人幫我:這就是其中之一:(我想刪除tmp目錄在當前工作目錄)如何用C++刪除目錄及其中的所有文件?

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); 
} 

回答

-3

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; 
} 
+0

OP詢問目錄 – fazo

+0

-1,沒有做什麼要求。 – MSalters

+1

OP和我顯然講一種祕密語言,因爲他接受:) – JAM

2
+0

你能告訴我怎麼做?例如一些代碼? – fali

+1

@fali boost :: filesystem :: remove_all(「directory」);開始將自己與助推世界聯繫起來。大多數時候重新創造輪子並不聰明。 – flumpb

2

你應該看看Boost Filesystem Library,它提供了許多使這種事情更容易的功能。鏈接頁面上的示例代碼與您想要完成的內容非常相似(它遞歸搜索目錄而不是遞歸地刪除內容)。

0

如果你不想使用升壓,你可以做到這一點

rm -r "folder name" 
相關問題