QFile example 1: copying file and checking if the copy exists is a QFile example.
Operating system(s) or programming environment(s)
- Lubuntu 15.04 (vivid)
- Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
Qt project file: ./CppQFileExample1/CppQFileExample1.pro
exists(../../DesktopApplication.pri) { include(../../DesktopApplication.pri) } !exists(../../DesktopApplication.pri) { QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets win32 { greaterThan(QT_MAJOR_VERSION, 4): QT += svg } TEMPLATE = app CONFIG(debug, debug|release) { message(Debug mode) } CONFIG(release, debug|release) { message(Release mode) DEFINES += NDEBUG NTRACE_BILDERBIKKEL } QMAKE_CXXFLAGS += -std=c++1y -Wall -Wextra -Weffc++ unix { QMAKE_CXXFLAGS += -Werror } } exists(../../Libraries/Boost.pri) { include(../../Libraries/Boost.pri) } !exists(../../Libraries/Boost.pri) { INCLUDEPATH += \ ../../Libraries/boost_1_55_0 } SOURCES += main.cpp
#include <cassert> #include <cstdio> #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QFile> #pragma GCC diagnostic pop int main(int /* argc */, char* argv[]) { //Copy executable (argv[0] is this executable its full path) QFile me(argv[0]); const std::string temp_filename = "tmp"; me.copy(temp_filename.c_str()); //Check that copy exists assert(QFile::exists(temp_filename.c_str())); //Delete the copy std::remove(temp_filename.c_str()); assert(!QFile::exists(temp_filename.c_str())); }