Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) DeleteFile

 

DeleteFile is a file I/O code snippet to delete a file.

 

#include <cassert>
#include <cstdio>
#include <fstream>

int main()
{
  const std::string filename = "tmp.txt";
  {
    //Check that file is not present
    std::ifstream file(filename.c_str());
    assert(!file.is_open());
  }
  {
    //Create file
    std::ofstream file(filename.c_str());
  }
  {
    //Check that file is present
    std::ifstream file(filename.c_str());
    assert(file.is_open());
  }
  {
    //Delete file
    std::remove(filename.c_str());
  }
  {
    //Check that file is not present
    std::ifstream file(filename.c_str());
    assert(!file.is_open());
  }
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict