(C++) StdRenameExample1

February 24, 2017 · View on GitHub

 

 

 

 

 

(C++) StdRenameExample1

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • STL STL: GNU ISO C++ Library, version 4.9.2

 

 

 

 

 

Qt project file: ./CppStdRenameExample1/CppStdRenameExample1.pro

 


TEMPLATE = app CONFIG += console CONFIG -= app_bundle CONFIG -= qt SOURCES += main.cpp

 

 

 

 

 

./CppStdRenameExample1/main.cpp

 


#include <cassert> #include <fstream> ///Determines if a filename is a regular file ///From http://www.richelbilderbeek.nl/CppIsRegularFile.htm bool IsRegularFile(const std::string& filename) {   std::fstream f;   f.open(filename.c_str(),std::ios::in);   return f.is_open(); } int main() {   const std::string before = "a.tmp";   const std::string after  = "b.tmp";   //Delete possible previous files   std::remove(before.c_str());   std::remove(after.c_str());   assert(!IsRegularFile(before));   assert(!IsRegularFile(after));   //Create before   {     std::ofstream f(before.c_str());   }   assert( IsRegularFile(before));   assert(!IsRegularFile(after));   //Rename before to after   {     std::rename(before.c_str(),after.c_str());   }   assert(!IsRegularFile(before));   assert( IsRegularFile(after));   //Delete files   std::remove(before.c_str());   std::remove(after.c_str());   assert(!IsRegularFile(before));   assert(!IsRegularFile(after)); }