Go back to Richel Bilderbeek's homepage.

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






(C++) RenameFile


Technical facts


Operating system(s) or programming environment(s)


Project type:

C++ standard:


Libraries used:






Qt project file: ./CppRenameFile/CppRenameFile.pro


CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp








#include <cassert>
#include <fstream>
#include <string>

bool IsRegularFile(const std::string& filename);

///Delete a file
//From http://www.richelbilderbeek.nl/CppDeleteFile.htm
void DeleteFile(const std::string& filename)


///Determines if a filename is a regular file
///From http://www.richelbilderbeek.nl/CppIsRegularFile.htm
bool IsRegularFile(const std::string& filename)
  std::fstream f;
  return f.is_open();

///Renames a file
//From http://www.richelbilderbeek.nl/CppRenameFile.htm
void RenameFile(const std::string& from, const std::string& to)
  assert( IsRegularFile(from) && "Cannot rename a non-existing file");
  assert(!IsRegularFile(to)   && "Cannot rename to an existing file");
  assert( IsRegularFile(to)  );

int main()
  const std::string filename_from = "tmp.txt";
  const std::string filename_to   = "tmp2.txt";

  //Delete possible old temporary files

  //Create new file
    std::ofstream f(filename_from.c_str());
    f << "TMP";

  //Only filename_from will exist
  assert( IsRegularFile(filename_from));


  //Only filename_to will exist
  assert( IsRegularFile(filename_to));







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

Go back to Richel Bilderbeek's homepage.


Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml