今天做了一个复制文件的小程序。
如下
// TODO: 在此添加控件通知处理程序代码
    CFileDialog  *file = new  CFileDialog (TRUE) ;
TCHAR *path = new TCHAR[1000] ;
CString                   str ;
 CString str5                 ;
::ZeroMemory (path, 1000) ;

GetModuleFileName (NULL, path, 1000) ;
//GetCurrentDirectory (1000, path) ;
file->DoModal ()                 ;
str = file->GetPathName()        ;    TCHAR FilePath[256] = {0};
    TCHAR Drive[4], Dir[128], FileName[132], Ext[20];    GetModuleFileName(NULL, FilePath, _countof(FilePath));
    _tsplitpath_s(FilePath, Drive, _countof(Drive), Dir, _countof(Dir), 
        FileName, _countof(FileName), Ext, _countof(Ext));
    _stprintf_s(FilePath, _countof(FilePath), TEXT("%s%s"),
        Drive, Dir);
//::MessageBox (this->m_hWnd, FilePath, NULL, 0) ; CString    *str1 = new CString(FilePath) ;
  int n = str.GetLength();    
             //获取宽字节字符的大小,大小是按字节计算的
      int len = WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),NULL,0,NULL,NULL); 
             //为多字节字符数组申请空间,数组大小为按字节计算的宽字节字节大小   //以字节为单位 
            //宽字节编码转换成多字节编码
 char  *buffer = new char[len+1] ;
     WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),buffer,len,NULL,NULL); 
             buffer [len] = '\0';   //多字节字符以'\0'结束 
   
DWORD   dwNum = MultiByteToWideChar (CP_ACP, 0, buffer, -1, NULL, 0) ;
    TCHAR *pwText =  new TCHAR[dwNum+1];
MultiByteToWideChar (CP_ACP, 0, buffer, dwNum, pwText, dwNum+1) ;
//pwText[dwNum] = L" " ;
::ZeroMemory (FileName, 132) ;
::ZeroMemory (Ext,      20 ) ;
::ZeroMemory (Dir,      128 ) ;
::ZeroMemory (Drive,      4 ) ;

_tsplitpath_s(pwText, Drive, _countof(Drive), Dir, _countof(Dir), 
        FileName, _countof(FileName), Ext, _countof(Ext));
    _stprintf_s(pwText, len+1, TEXT("%s%s"),
        Drive, Dir);
   CString    *str2 = new CString(FileName) ;
CString    *str3 = new CString(Ext) ; *str1 = *str1+TEXT("\\墙纸成品\\")+*str2+*str3  ;
//BOOL  bl = MoveFile (TEXT("F:\\122.bmp"), TEXT("C:\\122.bmp")) ;
BOOL  bl = CopyFile (path, *str1, FALSE) ;
//BOOL   bl = MoveFile (path, *str1) ;
if(bl)
{
::MessageBox (this->m_hWnd, TEXT("成功copy"), NULL, 0) ;
}
else
{
::MessageBox (this->m_hWnd, TEXT("失败copy"), NULL, 0) ;
} delete [] path   ;
delete [] pwText ;
delete [] buffer ;
delete    str1   ;
delete file      ;
复制成功,可是复制后的文件为什么全部都限定在48kb?  而且也都不能打开了  请教原因!!!

解决方案 »

  1.   

    一个函数:输入含全路径的文件名,Src-源文件 Dst-目标文件 // 合并文件
    BOOL UniteFile(CString Src,CString Dst,bool state=false);// 合并文件
    // state=false 将Src中数据接在Dst的尾部
    // state=true将覆盖Dst中的数据
    BOOL CFileManage::UniteFile(CString Src,CString Dst,bool state/*=false*/)
    {
    int const MaxLen=1024; // 每次读入的最大值 // 直接拷贝
    if(state)
    return CopyFile(Src,Dst,false); // 合并到尾部
    CFile fd; // 目标文件
    if(!fd.Open(Dst,CFile::modeNoInherit | CFile::modeWrite | CFile::typeBinary))
    {
    fd.Open(Dst,CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
    }
    fd.SeekToEnd(); // 设置到尾部 CFile fs(Src,CFile::modeRead | CFile::typeBinary); // 源文件
    char buf[MaxLen+2];
    int len=0; do {
    len=fs.Read(buf,MaxLen);
    fd.Write(buf,len);
    if(len<MaxLen)
    break;
    }while(1); fd.Close();
    fs.Close();
    return FALSE;
    }
      

  2.   

    复制文件居然写了这么多代码.........  CopyFile不会复制不完整
      

  3.   

    CopyFile()就像使用:
    copy src dest 这样,一句就好了