分割合并文件

工作以后基本不用C,全是C++,今天复习C语言中的文件操作,顺便写了个简单版本的文件分割和合并的程序。

// FileSplitter.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <boost/lexical_cast.hpp>
#include <string>
#include <assert.h>
#include <boost/foreach.hpp>
#include <vector>

namespace
{
    std::string DecoratePath(const std::string& dirPath)
    {
        char slash = '//';
        char slash_ex = '/';
        std::string path = dirPath;

        int nPathLen = static_cast<int>(path.length());
        if (nPathLen != 0 && (dirPath[nPathLen - 1] != slash || dirPath[nPathLen - 1] != slash_ex))
            path += slash;

        return path;
    }

    bool SplitFile(const char* fileName, int nBlockSize, const char* destPath)
    {
        assert(nBlockSize  > 0);
        if (nBlockSize <= 0)
            return false;

        FILE *pFile = fopen(fileName, "rb");
        if (pFile == NULL)
            return false;

        //how big
        fseek(pFile, 0, SEEK_SET);
        fseek(pFile, 0, SEEK_END);
        long nFileLen = ftell(pFile);

        //seeking back to the beginning
        fseek(pFile, 0, SEEK_SET);

        char *buf= new char[nBlockSize];

        int nCnts = 0;
        //not reach the end of file
        while (feof(pFile) == 0)
        {
            std::string fileName = DecoratePath(destPath);
            fileName = fileName + "part" + boost::lexical_cast<std::string>(++nCnts);
            FILE *pPartFile = fopen(fileName.c_str(), "wb");
            if (!pPartFile)
                continue;

            int nRead = fread(buf, sizeof(char), nBlockSize, pFile);
            fwrite(buf, sizeof(char), nRead, pPartFile);

            fclose(pPartFile);
        }

        delete []buf;

        fclose(pFile);

        return true;
    }

    typedef std::vector<std::string> Files_t;
    bool CombineFiles(const Files_t& files, const std::string& destFile)
    {
        bool fEmpty = files.empty();
        assert(!fEmpty);
        if (fEmpty)
            return false;

        FILE *pFile = fopen(destFile.c_str(), "wb");
        if (!pFile)
            return false;

        {
            const int nBuffer = 1024 * 1024 * 2; // 2M
            char *pBuf = new char[nBuffer];
            BOOST_FOREACH(const std::string& file, files)
            {
                FILE *pPartFile = fopen(file.c_str(), "rb");
                if (pPartFile == NULL)
                    continue;

                while (feof(pPartFile) == 0)
                {
                    int nRead = fread(pBuf, sizeof(char), nBuffer, pPartFile);
                    fwrite(pBuf, sizeof(char), nRead, pFile);
                }
               
                fclose(pPartFile);
            }
            delete []pBuf;
        }
       
        fclose(pFile);
    }
}


int _tmain(int argc, _TCHAR* argv[])
{
    //该文件是2k的,1k一个文件
    SplitFile("g://2.txt", 1024 * 1, "g://");

    Files_t files;
    files.push_back("g://part1");
    files.push_back("g://part2");

    CombineFiles(files, "g://new2.txt");

    return 0;
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值