1void CFileCutter::DoSplit() 2{ 3 int nCompleted = 0; //计数 4 CString strSourceFile = m_strSource; //取得全局变量赋值给局部变量,方便操作 5 CString strDestDir = m_strDest; 6 CFile sourceFile, destFile; 7 //打开文件 8 BOOL bOK = sourceFile.Open(strSourceFile, CFile::modeRead | CFile::shareDenyWrite | CFile::typeBinary); 9 if (!bOK)//打不开就报错退出 10 { 11 ::PostMessage(m_hWndNotify, WM_CUTTERSTOP, exitSourceErr, nCompleted); 12 return; 13 } 14 int nPos = 0; 15 //逐层创建文件夹(不存在就创建) 16 while ((nPos = strDestDir.Find('\\', nPos + 1)) != -1)//少了个括号 17 { 18 ::CreateDirectory(strDestDir.Left(nPos),NULL); 19 } 20 ::CreateDirectory(strDestDir, NULL); //创建最底层文件夹 21 if (strDestDir.Right(1) != '\\') 22 { 23 strDestDir += '\\'; 24 } 25 int nTotalFiles = sourceFile.GetLength() / m_uFileSize + 1;//判断切割后文件数 26 ::PostMessage(m_hWndNotify,WM_CUTTERSTART,nTotalFiles,TRUE); //设置进度条长度 27 const int c_page = 4 * 1024; //每次读取大小 28 char buff[c_page]; //缓冲区 29 DWORD dwRead; 30 31 CString sDestName; 32 int nPreCount = 1; 33 UINT uWriteBytes; 34 35 do 36 { 37 //设置文件名并创建文件 38 sDestName.Format(TEXT("%d_"), nPreCount); 39 sDestName += sourceFile.GetFileName(); 40 if (!destFile.Open(strDestDir + sDestName, CFile::modeWrite | CFile::modeCreate)) 41 { 42 PostMessage(m_hWndNotify, WM_CUTTERSTOP, exitDestErr, nCompleted); 43 sourceFile.Close(); 44 return; 45 } 46 uWriteBytes = 0; 47 do 48 { 49 //读写操作 50 if (!m_bContinue) //stopCutter函数 51 { 52 destFile.Close(); 53 sourceFile.Close(); 54 if (!m_bExitThread) //析构函数 55 { 56 ::PostMessage(m_hWndNotify, WM_CUTTERSTOP, exitUserForce, nCompleted); 57 } 58 return; 59 } 60 dwRead = sourceFile.Read(buff, c_page);//读 61 destFile.Write(buff, dwRead); //写 62 uWriteBytes += dwRead; //计数 63 64 } while (dwRead > 0 && uWriteBytes < m_uFileSize);//读出了数据并且一共写的数据量小于用户要求的数据量 65 destFile.Close(); //当前文件写完了,关闭 66 nCompleted = nPreCount++; //计数 67 ::PostMessage(m_hWndNotify, WM_CUTTERSTATUS, 0, nCompleted); 68 69 } while (dwRead > 0); //只要读出了数据就循环继续,直到源文件全部读出。 70 sourceFile.Close(); //最后关闭原文件 71 ::PostMessage(m_hWndNotify, WM_CUTTERSTOP, exitSuccess, nCompleted); 72}
C++切割文件
Wesley13
2021-10-11
1046 0 0
点赞
收藏
评论区
加载中...