如何实现分割和截取文件? 如何将一个大文件分割为多个小文件?如何从一个大文件中截取一部分内容?请指点一下思路,有源代码当然更好!谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ASSERT(m_file.m_hFile!=CFile::hFileNull); //获得文件长度以及用户指定的子文件大小 DWORD dwLength=m_file.GetLength(); DWORD dwPart=(DWORD)1024*GetDlgItemInt(IDC_SPIFILE); //子文件大小为0 if(dwPart==0) { AfxMessageBox(_T("切割文件大小不能为0!")); return; } //子文件比愿文件还大 else if(dwPart>dwLength) { AfxMessageBox(_T("切割文件比原文件大,将不做切割!")); return; } //子文件数将超过1000个 else if(dwPart<dwLength/1000) { AfxMessageBox(_T("切割文件太小了,总数应该小于1000!")); return; } //获得必要的内存空间 char *pBuf=new char[dwPart]; if(pBuf==NULL) { AfxMessageBox(_T("没有足够的内存来进行分割操作!")); return; } //获得用户的输入值 UpdateData(); int nCnt=1; CString strName; DWORD dwRead; CFile wrFile; //开始分割 while(1) { //从原文件中读取指定文件长度部分 dwRead=m_file.Read(pBuf,dwPart); //将子文件命名为“文件标题.子文件序号”的方式 strName.Format("%s.%d",m_strTitle,nCnt); wrFile.Open(strName,CFile::modeCreate|CFile::modeWrite); //将读得的部分写入新的文件 wrFile.Write(pBuf,dwRead); wrFile.Close(); //若读得的长度比要求的少则跳出 if(dwRead<dwPart) break; nCnt++; } //建立的子文件数是否合乎要求 BOOL bSuccess=(dwRead+(nCnt-1)*dwPart==dwLength); //创建自合并文件,实际上是一个批处理程序,它利用DOS中拷贝文件时可以合并文件的功能 //(利用DOS命令:copy filename1/b+filename2/b filename)可以以二进制的方式将文件filename //和filename2拷贝到同一个文件中而达到合并文件的目的。 CString strReport=_T(""); if(bSuccess&&m_bCom) { //按照“copy filename1/b+filename2/b+...+filenameN/b filename"的顺序写入批处理 strReport+=_T("copy "); for(int i=1;i<nCnt;i++) { strName.Format("%s.%d/b+",m_strTitle,i); strReport+=strName; } strName.Format("%s.%d/b ",m_strTitle,i); strReport+=strName; strReport+=m_strName; strName=m_strTitle+".bat"; wrFile.Open(strName,CFile::modeCreate|CFile::modeWrite); wrFile.Write((LPCTSTR)strReport,strReport.GetLength()); } //若用户要求查看分割结果 if(m_bReport) { strReport=m_strPath+"\r\n"; strReport+=_T("分割为下列文件:\r\n"); for(int i=1;i<=nCnt;i++) { strName.Format("%s.%d\r\n",m_strTitle,i); strReport+=strName; } //若分割成功 if(bSuccess) { strReport+=_T("分割成功!"); strName.Format(_T("\r\n生成自合并文件\r\n%s.bat"),m_strTitle); if(m_bCom) strReport+=strName; } else { //分割不成功 strReport+=_T("分割未成功!"); } } //用户不要求查看最后的报告 else { strReport=bSuccess?_T("分割成功!"):_T("分割不成功!"); } AfxMessageBox(strReport); See the sample below, FYI: http://www.vckbase.com/document/viewdoc.asp?id=293 CreateWindow的classname都有啥? 基于MFC的FTP上传下载功能的实现 得到IE地址栏内容的代码,为什么不好使? (100分)求解winmm.dll中句柄泄露解决方案 如何在我自己编的IE浏览器中实现打开网页的进度条显示,基类CHtmlView 定义接口方法时,如何定义数组类型 关于splitter window 谁有系统AND详细的介绍WINDOWS的消息的资料? 再问一次:COM的一个简单问题! 怎样打开多个视图? 如何或得DirectX的版本信息? 诸位有没有遇见程序运行不报错,却不能调试走单步的情况
DWORD dwLength=m_file.GetLength();
DWORD dwPart=(DWORD)1024*GetDlgItemInt(IDC_SPIFILE); //子文件大小为0
if(dwPart==0)
{
AfxMessageBox(_T("切割文件大小不能为0!"));
return;
} //子文件比愿文件还大
else if(dwPart>dwLength)
{
AfxMessageBox(_T("切割文件比原文件大,将不做切割!"));
return;
} //子文件数将超过1000个
else if(dwPart<dwLength/1000)
{
AfxMessageBox(_T("切割文件太小了,总数应该小于1000!"));
return;
} //获得必要的内存空间
char *pBuf=new char[dwPart];
if(pBuf==NULL)
{
AfxMessageBox(_T("没有足够的内存来进行分割操作!"));
return;
} //获得用户的输入值
UpdateData();
int nCnt=1;
CString strName;
DWORD dwRead;
CFile wrFile; //开始分割
while(1)
{
//从原文件中读取指定文件长度部分
dwRead=m_file.Read(pBuf,dwPart);
//将子文件命名为“文件标题.子文件序号”的方式
strName.Format("%s.%d",m_strTitle,nCnt);
wrFile.Open(strName,CFile::modeCreate|CFile::modeWrite);
//将读得的部分写入新的文件
wrFile.Write(pBuf,dwRead);
wrFile.Close(); //若读得的长度比要求的少则跳出
if(dwRead<dwPart)
break;
nCnt++;
} //建立的子文件数是否合乎要求
BOOL bSuccess=(dwRead+(nCnt-1)*dwPart==dwLength); //创建自合并文件,实际上是一个批处理程序,它利用DOS中拷贝文件时可以合并文件的功能
//(利用DOS命令:copy filename1/b+filename2/b filename)可以以二进制的方式将文件filename
//和filename2拷贝到同一个文件中而达到合并文件的目的。
CString strReport=_T("");
if(bSuccess&&m_bCom)
{
//按照“copy filename1/b+filename2/b+...+filenameN/b filename"的顺序写入批处理
strReport+=_T("copy ");
for(int i=1;i<nCnt;i++)
{
strName.Format("%s.%d/b+",m_strTitle,i);
strReport+=strName;
}
strName.Format("%s.%d/b ",m_strTitle,i);
strReport+=strName;
strReport+=m_strName;
strName=m_strTitle+".bat";
wrFile.Open(strName,CFile::modeCreate|CFile::modeWrite);
wrFile.Write((LPCTSTR)strReport,strReport.GetLength());
} //若用户要求查看分割结果
if(m_bReport)
{
strReport=m_strPath+"\r\n";
strReport+=_T("分割为下列文件:\r\n");
for(int i=1;i<=nCnt;i++)
{
strName.Format("%s.%d\r\n",m_strTitle,i);
strReport+=strName;
} //若分割成功
if(bSuccess)
{
strReport+=_T("分割成功!");
strName.Format(_T("\r\n生成自合并文件\r\n%s.bat"),m_strTitle);
if(m_bCom)
strReport+=strName;
}
else
{
//分割不成功
strReport+=_T("分割未成功!");
}
} //用户不要求查看最后的报告
else
{
strReport=bSuccess?_T("分割成功!"):_T("分割不成功!");
}
AfxMessageBox(strReport);