zoukankan      html  css  js  c++  java
  • C++切割文件

    void CFileCutter::DoSplit()
    {
        int nCompleted = 0;   //计数
        CString strSourceFile = m_strSource;  //取得全局变量赋值给局部变量,方便操作
        CString strDestDir = m_strDest;
        CFile sourceFile, destFile;
        //打开文件
        BOOL bOK = sourceFile.Open(strSourceFile, CFile::modeRead | CFile::shareDenyWrite | CFile::typeBinary);
        if (!bOK)//打不开就报错退出
        {
            ::PostMessage(m_hWndNotify, WM_CUTTERSTOP, exitSourceErr, nCompleted);
            return;
        }
        int nPos = 0;
        //逐层创建文件夹(不存在就创建)
        while ((nPos = strDestDir.Find('\', nPos + 1)) != -1)//少了个括号
        {
            ::CreateDirectory(strDestDir.Left(nPos),NULL);
        }
        ::CreateDirectory(strDestDir, NULL);  //创建最底层文件夹
        if (strDestDir.Right(1) != '\')   
        {
            strDestDir += '\';
        }
        int nTotalFiles = sourceFile.GetLength() / m_uFileSize + 1;//判断切割后文件数
        ::PostMessage(m_hWndNotify,WM_CUTTERSTART,nTotalFiles,TRUE); //设置进度条长度
        const int c_page = 4 * 1024;  //每次读取大小
        char buff[c_page]; //缓冲区
        DWORD dwRead;
    
        CString sDestName;
        int nPreCount = 1;
        UINT uWriteBytes;
    
        do 
        {
            //设置文件名并创建文件
            sDestName.Format(TEXT("%d_"), nPreCount);
            sDestName += sourceFile.GetFileName();
            if (!destFile.Open(strDestDir + sDestName, CFile::modeWrite | CFile::modeCreate))
            {
                PostMessage(m_hWndNotify, WM_CUTTERSTOP, exitDestErr, nCompleted);
                sourceFile.Close();
                return;
            }
            uWriteBytes = 0;
            do
            {
                //读写操作
                if (!m_bContinue)  //stopCutter函数
                {
                    destFile.Close();
                    sourceFile.Close();
                    if (!m_bExitThread)  //析构函数
                    {
                        ::PostMessage(m_hWndNotify, WM_CUTTERSTOP, exitUserForce, nCompleted);
                    }
                    return;
                }
                dwRead = sourceFile.Read(buff, c_page);//
                destFile.Write(buff, dwRead); //
                uWriteBytes += dwRead;  //计数
    
            } while (dwRead > 0 && uWriteBytes < m_uFileSize);//读出了数据并且一共写的数据量小于用户要求的数据量
            destFile.Close();    //当前文件写完了,关闭
            nCompleted = nPreCount++;  //计数
            ::PostMessage(m_hWndNotify, WM_CUTTERSTATUS, 0, nCompleted);
    
        } while (dwRead > 0); //只要读出了数据就循环继续,直到源文件全部读出。
        sourceFile.Close(); //最后关闭原文件
        ::PostMessage(m_hWndNotify, WM_CUTTERSTOP, exitSuccess, nCompleted);
    }
  • 相关阅读:
    c++模板使用
    配置文件读取(2-2)读取yml
    全景拼接学习-原理篇 (4) 基本过程
    全景拼接学习-原理篇 (3) 从对极几何 单相机拍摄的画面估计运动轨迹
    全景拼接学习-原理篇 (2) 单张图片 单相机成像模型
    全景拼接学习-原理篇 (1) 两张图片之间关系计算 单应性Homograph估计
    js之async和await
    mysql之GORM接口
    casbin应用
    jQuery
  • 原文地址:https://www.cnblogs.com/cteng-common/p/cutterfile.html
Copyright © 2011-2022 走看看