-
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!
-
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.
-
Re: CopyFile() Function
Problem solved, I forgot to change the setting in Visual Studio to not use unicode character set.
Thanks again
-
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.
-
Re: CopyFile() Function
Use the code tags, you'll get much more help since the code will be much more legible.