100分急求复制文件问题

chrc1122 2011-10-16 06:31:10

今天做了一个复制文件的小程序。
如下
// 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? 而且也都不能打开了 请教原因!!!
...全文
89 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
abee23 2011-10-16
  • 打赏
  • 举报
回复
CopyFile()就像使用:
copy src dest 这样,一句就好了。。。

cczlp 2011-10-16
  • 打赏
  • 举报
回复
复制文件居然写了这么多代码......... CopyFile不会复制不完整
笨笨仔 2011-10-16
  • 打赏
  • 举报
回复
一个函数:输入含全路径的文件名,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;
}
chrc1122 2011-10-16
  • 打赏
  • 举报
回复
自己顶个 应该是个简单问题啊!! 怎么没人回呢

16,547

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧