如何使用c++使用流的方法删除文件

how to delete the file using method of ofstream using c++?

本文关键字:方法 删除 文件 何使用 c++      更新时间:2023-10-16

我正在使用流输出到一个文件,然后我想在程序结束时擦除该文件。是否有fstream的方法或任何允许我删除文件的方法?

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