我要实现文件的拷贝,这么做呢?
解决方案 »
- VC能自定义float类型么
- 在mfcz中工具条的停靠风格中有一条“CBRS_FLOAT_MULTI”解析是允许在一单边窗口内存在多个可移动的控制条”其中的“单边窗口”什么意思?
- no appropriate default constructor available
- VC下使用ms chart的问题,求助!
- 请教一个FTP问题
- 小弟想学习VC请各位兄长介绍一本入门的书籍
- 程序初始化时的问题,100分
- 多线程同步问题询问?谢谢!
- 求助,别人调用dll打开我的非模态对话框,为什么PreTranslateMessage获取不到消息了
- CSocket 类编程时,我 遇到了困难,帮我吧
- 请教关于超大文件处理问题
- WTL的下载
LPCTSTR lpExistingFileName, // name of an existing file
LPCTSTR lpNewFileName, // name of new file
BOOL bFailIfExists // operation if file exists
);
bFailIfExists [in]If this parameter is TRUE and the new file already exists, the function fails. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds.
BOOL CopyFile(
LPCTSTR lpExistingFileName, // name of an existing file
LPCTSTR lpNewFileName, // name of new file
BOOL bFailIfExists // operation if file exists
);
bool C...Dlg::CopyFile(CString lpSourceFile,lpDestFile,int nBufSize)
{
CFile Out,In;
int nFileSize;
char* pBuf;
if(!In.Open(lpSourceFile,CFile::modeRead))
{
AfxMessageBox("打开源文件失败");
return false;
}
if(!Out.Open(lpDestFile,CFile::modeWrite | CFile::modeCreate))
{
AfxMessageBox("创建目标文件失败");
return false;
}
lpBuf=new char[nBufSize];
if(lpBuf==NULL)
{
AfxMessageBox("缓冲分配失败");
return false;
}
CFileStatus rStatus;
In.GetStatus(lpSourceFile,rStatus);//获取源文件属性
nFileSize=In.GetLength();
while(nFileSize>0)//开始拷贝
{
int nSize=nBufSize;
if(nSize>nFileSize) nSize=nFileSize;
try{//读取文件
In.Read(lpBuf,nSize);//从源文件读取nSize字节
}catch(CFileException* e){
char* lpMsg;
if(FormatMessage(FORMAT_MESSAGE_ALLOCATION_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,e->m_1OsError,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPSTR)&lpMsg,0,NULL)>0)
{
AfxMessageBox(lpMsg);
LocalFree(lpMsg);
}
e->Delete();
return false;
}
try{//输出文件
Out.Write(lpBuf,nSize);
}catch(CFileException* e){
char* lpMsg;
if(FormatMessage(FORMAT_MESSAGE_ALLOCATION_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,e->m_lOsError,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPSTR)&lpMsg,0,NULL)>0)
{
AfxMessageBox(lpMsg);
LocalFree(lpMsg);
}
e->Delete();
return false;
}
nFileSize-=nSize;
}
Out.Close();
CFile::SetStatus(lpDestFile,rStatus);//同性设置
delete[] lpBuf;
return true;
}
LPCTSTR lpExistingFileName, // name of an existing file
LPCTSTR lpNewFileName, // name of new file
BOOL bFailIfExists // operation if file exists
);这种方法应该是最简单的了。2 bool CopyFile(char * szExitFILE, char * szNewFiel)
{
std::ifstream in_file(szExitFiel);
std::ofstream out_file(szNewFile);if(in_file)
{
return false;
}
if(out_file)
{
return false;
}char ch;
while(in_file.get(ch))
{
out_file.put(ch);
}
return true;
}
设置SHFILEOPSTRUC就行。如下:
SHFILEOPSTRUCT FileOp;
FileOp.hwnd=m_hWnd;
FileOp.wFunc=FO_COPY;
//执行文件拷贝
FileOp.pFrom=strScr;
FileOp.pTo=strDst;
FileOp.fFlags=FOF_ALLOWUNDO ;
FileOp.hNameMappings=NULL;
FileOp.lpszProgressTitle=strTitle;
nOk=SHFileOperation(&FileOp);
if(nOk)
TRACE("There is an error: %d\n",nOk);
else
TRACE("SHFileOperation finished successfully\n");