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>
int main()
            if ( rename("c:\a\abc.txt","c:\b\abc.txt")
                    perror( NULL );
                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?


Edited by Dani: Formatting fixed

7 Years
Discussion Span
Last Post by WaltP

First off you need to decide whether you're going to use the copy (Windows) or cp (Linux, Unix) shell command for copying the file or whether you're manually going to write the file copy routine in C++.


well as far as the copy three times your going to need a loop like do{code} until... give button pressed... secondly ur going to want the file to be appended each time it was pasted so ur going to want to use filename.append ++ which can be into a loop also so u can append the filename for everytime the loop repeats itself


The rename() function renames, does not copy. You need to open 2 files: the original file for reading, the new file for writing. Then read the old file and write to the new file.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.