-1

I would like to rename and retain the source file by using C++. I use this for renaming the file.

For eg:

rename(source_file.txt, destination_file.txt);

In this, I would like to retain the source_file.txt. By default, this function deletes the source_file and saves it as destination_file.

Paul R
  • 208,748
  • 37
  • 389
  • 560
Santhosh
  • 35
  • 4
  • 2
    So you don't want to rename it, just copy it? Open source file, create new file(that will be a copy), then simply read line by line or whatever and copy original file to new file. – Rorschach Nov 30 '16 at 11:27
  • 1
    It doesn't delete the file. It renames the file. Same file, new name. That is the purpose of that function. If you want to keep the source, you must copy it. – Gerhardh Nov 30 '16 at 11:40

1 Answers1

1

Probably the easiest, most reliable and most portable method is to use boost::filesystem::copy_file():

#include <boost/filesystem.hpp>

using namespace boost::filesystem;

copy_file("source_file.txt", "destination_file.txt", copy_option::overwrite_if_exists);
Paul R
  • 208,748
  • 37
  • 389
  • 560