(C++) CopyFile
February 24, 2017 · View on GitHub
(C++) CopyFile
CopyFile is a file I/O code snippet to copy a file.
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
STL: GNU ISO C++ Library, version
4.9.2
Qt project file: ./CppCopyFile/CppCopyFile.pro
include(../../ConsoleApplication.pri) #Or use the code below # QT += core # QT += gui # greaterThan(QT_MAJOR_VERSION, 4): QT += widgets # CONFIG += console # CONFIG -= app_bundle # TEMPLATE = app # CONFIG(release, debug|release) { # DEFINES += NDEBUG NTRACE_BILDERBIKKEL # } # QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ # unix { # QMAKE_CXXFLAGS += -Werror # } include(../../Libraries/Boost.pri) #Or use the code below # win32 { # INCLUDEPATH += \ # ../../Libraries/boost_1_54_0 # } SOURCES += main.cpp
./CppCopyFile/main.cpp
#include <cassert> #include <fstream> #include <stdexcept> #ifdef BOOST_H #include <boost/filesystem.hpp> #endif #ifdef VCL_H #include <SysUtils.hpp> #endif ///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(); } //From http://www.richelbilderbeek.nl/CppCopyFile.htm void CopyFileStl(const std::string& fileNameFrom, const std::string& fileNameTo) { assert(IsRegularFile(fileNameFrom)); if(IsRegularFile(fileNameTo)) { throw std::logic_error("Cannot copy over an existing file"); } std::ifstream in (fileNameFrom.c_str()); std::ofstream out(fileNameTo.c_str()); out << in.rdbuf(); out.close(); in.close(); } #ifdef BOOST_H //From http://www.richelbilderbeek.nl/CppCopyFile.htm void CopyFileBoost(const std::string& from, const std::string& to) { assert(IsRegularFile(fileNameFrom)); //Boost will check if the copy is made over an existing file boost::filesystem::copy_file(from,to); } #endif #ifdef VCL_H //From http://www.richelbilderbeek.nl/CppCopyFile.htm void CopyFileVcl(const std::string& from, const std::string& to, const bool failIfExists) { //Use VCL its CopyFile CopyFile("FileFrom.txt","FileTo.txt",failIfExists); } #endif int main(int, char* argv[]) { const std::string tmp_filename = "temp.txt"; //Delete file (in case it exists) std::remove(tmp_filename.c_str()); //Copy file CopyFileStl(argv[0],tmp_filename); assert(IsRegularFile(tmp_filename)); //Clean up temp file by deleting it std::remove(tmp_filename.c_str()); }