Click to See Complete Forum and Search --> : copying file from one location to another location


priya_esu
October 10th, 2009, 02:42 PM
Hi,

I need to copy a file from one location say c:\a\abc.txt to c:\b\abc.txt. I used the following below code

#include <stdio.h>
#include<fstream>
int main()
{
if ( rename("c:\a\abc.txt","c:\b\abc.txt")
perror( NULL );
system("pause");
return 0;
}

when i ran this code, the original file in the folder "a" gets deleted. I do not want the original file to be deleted. what should i modify in this code so the original file remains undeleted.
and my next question is, i want to copy the original file 3 times and paste it in the new location 3 times, and each time while pasting i want the file name like this "abc1.txt", "abc2.txt", "abc3.txt"...and so on till i give a condition, so how to implement this?

Thanks
Priya

GameZelda
October 10th, 2009, 04:31 PM
Solution 1 (requires boost): Use copy_file() (http://www.boost.org/doc/libs/1_40_0/libs/filesystem/doc/reference.html#Header-filesystem-synopsis) from the filesystem library.

Solution 2 (requires Windows API):Use CopyFile() (http://msdn.microsoft.com/en-us/library/aa363851%28VS.85%29.aspx).

Solution 3: Use the C (fopen(), fread(), fwrite(), fclose() (http://www.cplusplus.com/reference/clibrary/cstdio/)) or C++ (fstream:: open(), fstream::read(), fstream::write() (http://www.cplusplus.com/reference/iostream/fstream)) standard library to copy the contents of the input file to the output file. WARNING: This will not copy the file attributes or the alternate streams (for NTFS).

golanshahar
October 19th, 2009, 01:21 AM
Hi,

I need to copy a file from one location say c:\a\abc.txt to c:\b\abc.txt. I used the following below code

#include <stdio.h>
#include<fstream>
int main()
{
if ( rename("c:\a\abc.txt","c:\b\abc.txt")
perror( NULL );
system("pause");
return 0;
}

when i ran this code, the original file in the folder "a" gets deleted. I do not want the original file to be deleted. what should i modify in this code so the original file remains undeleted.
and my next question is, i want to copy the original file 3 times and paste it in the new location 3 times, and each time while pasting i want the file name like this "abc1.txt", "abc2.txt", "abc3.txt"...and so on till i give a condition, so how to implement this?

Thanks
Priya

Look at ::SHFileOperation() (http://msdn.microsoft.com/en-us/library/bb762164(VS.85).aspx).

Cheers