Copy an existing file at runtime using a relative path

I have a method which saves data into a binary file. I'm trying to unit test this method. That's why I made myself a controller binary file with the data I'm supposed to save using my method.

My folder tree looks like this :

    └─── header.h
│   └─── source.cpp
        │   └── controller.bin
        └─── unitary_test.cpp

When running my test in unitary_test.cpp, I want to copy controller.bin to the temporary folder I create, the folder where I'm creating the file using my method under test.

The thing is that I can't manage to copy this file using relative path as my file is not found.

QTemporaryDir dir;
std::string controllerPath = "controller.bin";
QFile::copy(QString::fromStdString("./data/" + controllerPath), QDir(dir.path()).filePath(QString::fromStdString(controllerPath)));

Is there a way I can do this at runtime using a relative path?


  • Relative path must be relative to runtime current directory. Probable you execute you unitary_test from another current directory where the relative path data/controller.bin doesn't exist.

    Where is "unitary_test.exe" located? As far as I know, Qt Creator produces build results into the separate directory (not into the source directory)

    Find unitary_test.exe and copy "data" dir to it's directory. Astually it should be done in your build script (e.g. CMakeLists.txt)