2016-04-29 536 views
4

我正在使用ofstream輸出到一個文件,然後我想在程序結束時將其刪除。是否有fstream或任何可以刪除文件的方法?如何使用C++使用ofstream方法刪除文件?

+4

[文件系統庫(http://en.cppreference.com/w/cpp/experimental/fs)提供C + +17。現在,您可以使用[Boost](http://www.boost.org/doc/libs/1_60_0/libs/filesystem/doc/index.htm)或使用特定於平臺的apis。 –

回答

5

std :: fstream不提供文件系統操作,它只提供文件操作。

您可以使用C stdio remove,應與大多數編譯器工作:

/* remove example: remove myfile.txt */ 
#include <stdio.h> 

int main() 
{ 
    if(remove("myfile.txt") != 0) 
    perror("Error deleting file"); 
    else 
    puts("File successfully deleted"); 
    return 0; 
}