1.先包含头文件#include <Windows.h>
2.函数CopyFile的原型如下
BOOL CopyFile( LPCTSTR lpExistingFileName, // pointer to name of an existing file LPCTSTR lpNewFileName, // pointer to filename to copy to BOOL bFailIfExists // flag for operation if file exists );
用法
CopyFile("e:\\abb.txt","d:\\zhengyong.txt",FALSE);即可。 第三个参数有以下说明: 如果设为TRUE(非零),那么一旦目标文件已经存在,则函数调用会失败。否则目标文件会被覆盖掉。 3.如果传入的第二个和第三个参数的类型为string类型,会报错,解决办法参考: http://stackoverflow.com/questions/1200188/how-to-convert-stdstring-to-lpcstr
在源文件中 添加函数
LPWSTR ConvertToLPWSTR( const std::string& s ) { LPWSTR ws = new wchar_t[s.size()+1]; // +1 for zero at the end copy( s.begin(), s.end(), ws ); ws[s.size()] = 0; // zero at the end return ws; } 用法 CopyFile(ConvertToLPWSTR(str1),ConvertToLPWSTR(str2),FALSE);