CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2009
    Posts
    1

    copying file from one location to another location

    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

  2. #2
    Join Date
    Dec 2007
    Posts
    69

    Re: copying file from one location to another location

    Solution 1 (requires boost): Use copy_file() from the filesystem library.

    Solution 2 (requires Windows API):Use CopyFile().

    Solution 3: Use the C (fopen(), fread(), fwrite(), fclose()) or C++ (fstream:: open(), fstream::read(), fstream::write()) 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).

  3. #3
    Join Date
    May 2005
    Posts
    4,954

    Re: copying file from one location to another location

    Quote Originally Posted by priya_esu View Post
    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().

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured