How to delete a file using the ofstream method using C ++?

I use instream to output to a file, which I then want to delete at the end of my program. Is there a fstream way or anything that allows me to delete a file?

+4
source share
1 answer

std :: fstream does not provide file system operations; it only provides file operations.

You can use C stdio remove , which should work with most compilers:

/* 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;
}
+5
source

All Articles