Search code examples
c++windowsc++11file-renamefile-copying

CopyFile from one directory to another (C++)


i'm working on Windows and i'm trying to simply move a file from one directory to another. I use the method "CopyFile" but i have an issue whith the name of the file copied; i'm trying this:

#include <windows.h>
#include <stdio.h>

std::string source_path = "C:/ProgramData/RTD02/versions/versions.txt";
std::string destination_path = "C:/ProgramData/RTD02/principale.txt";
CopyFile(source_path.c_str(), destination_path.c_str(), 0);

In fact the content of the file "versions.txt" is well copied into the file "principale.txt". But what i want is also to change the name of file "principale.txt" to "versions.txt", i tried this one but it didn't work :

 rename(destination_path.c_str(), source_path.c_str());

Thanks,


Solution

  • The answer is that your destination string needs to be, well the destination.

    std::string destination_path = "C:/ProgramData/RTD02/versions.txt";
    

    If you meant to replace an existing file you then follow up a successful copy with DeleteFile the principale.txt