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

    Question CopyFile() Function

    Hi, Im trying to write a program that will simply copy a text file from one folder located on my C drive, to another, however, the CopyFile() function does not copy the file, and after tracing the error with GetLastError, I get a "Can not copy file" error. I have tried altering my directory by using double backslashes, however, it does not seem to work either. Here is my code:

    #include <iostream>
    using namespace std;
    #include <windows.h>

    int main()
    {
    if ( CopyFile( (LPCWSTR("c:\Documents and Settings\User\Desktop\bla\Doc1.txt")),
    (LPCWSTR("c:\Documents and Settings\User\Desktop\bla2\Doc1.txt")),
    0) )
    {

    }
    else
    {
    cout << "Could not copy file";
    cout << GetLastError();
    }

    system("pause");
    return 0;
    }

    Any help is appreciated. Thanks!

  2. #2
    Join Date
    Nov 2006
    Posts
    1,611

    Re: CopyFile() Function

    I have to guess.

    Perhaps bla2 doesn't exist?

    You'd have to make the destination directory before you perform the copy.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  3. #3
    Join Date
    Jul 2009
    Posts
    2

    Re: CopyFile() Function

    Problem solved, I forgot to change the setting in Visual Studio to not use unicode character set.

    Thanks again
    Last edited by TeachMeProgPl0x; July 3rd, 2009 at 12:19 AM.

  4. #4
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: CopyFile() Function

    This is the syntax if you want to use UNICODE.
    if ( CopyFile( (_T("c:\\Documents and Settings\\User\\Desktop\\bla\\Doc1.txt")),
    (_T("c:\\Documents and Settings\\User\\Desktop\\bla2\\Doc1.txt")),
    0) )

    Actually it will work both for unicode and non-unicode builds.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  5. #5
    Join Date
    Nov 2005
    Posts
    281

    Re: CopyFile() Function

    Use the code tags, you'll get much more help since the code will be much more legible.

Tags for this Thread

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