-1

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,

Jaziri Rami
  • 147
  • 1
  • 12

2 Answers2

2

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

acraig5075
  • 10,588
  • 3
  • 31
  • 50
  • Thanks @acraig5075 ! it was because of the paths i use in the rename() method; that's the correction: rename("C:/ProgramData/RTD02/principale.txt","C:/ProgramData/RTD02/versions.txt"); – Jaziri Rami Mar 10 '20 at 11:17
0

The Windows function to move a file is called just that, MoveFile. Works just like CopyFile.

MSalters
  • 173,980
  • 10
  • 155
  • 350