美文网首页C语言C++
分割和合并文件

分割和合并文件

作者: 一书and一世界 | 来源:发表于2016-12-16 16:31 被阅读94次

题目描述

假设要备份一个巨大的文件(比如10GB AVI文件)到CD-R,可以把文件分割成几个小的文件,然后逐个备份。编写一个实用程序,它能将大文件分割成小文件。程序提示用户输入原文件名和每个小文件的字节数。下面是样例运行:

Enter a source file name: c:\exercise.zip  ~Enter
Enter the number of bytes in each smaller file: 9343400  ~Enter
File c:\exercise.zip.0 produced
File c:\exercise.zip.1 produced
File c:\exercise.zip.2 produced
File c:\exercise.zip.3 produced
Split Done

编写一个实用程序,合并文件到一个新的文件。程序提示用户输入源文件的个数、每个源文件的名字及目标文件名字。下面是样例运行:

Enter the number of source files: 4  ~Enter
Enter a source file:   c:\exercise.zip.0  ~Enter
Enter a source file:   c:\exercise.zip.1  ~Enter
Enter a source file:   c:\exercise.zip.2  ~Enter
Enter a source file:   c:\exercise.zip.3  ~Enter
Enter a target file:   c:\temp.zip  ~Enter
Combine Done

C++代码

main.cpp

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

void splitFile(string path,long long smallFileBytes){
    fstream sourceFile(path.c_str(),ios::in|ios::binary);
    if (sourceFile.fail()){
        cout << "打开文件失败!" << endl;
        exit(1);
    }
    int numberOfFile = 0;
    char readBuffer[smallFileBytes];
    while(!sourceFile.eof()){
        string smallFileName = path;    //应该需要分割/才对,可是没时间弄了
        if (sourceFile.good()){
            numberOfFile++;
            sourceFile.read(readBuffer,smallFileBytes);
            readBuffer[sourceFile.gcount()] = '\0'; //可能没有写完整个readBuffer数组

            ostringstream ostring;
            ostring << smallFileName << numberOfFile;
            smallFileName = ostring.str();

            fstream resultFile(smallFileName.c_str(),ios::out | ios::binary);
            resultFile.write(readBuffer,sourceFile.gcount());
            cout << "File " << smallFileName << " produced." << endl;
        }
    }
    cout << "Split Done." << endl;
    sourceFile.close();
}

void mergeFile(int numOfFiles,string* fileArr,string resultFileName){

    fstream resultFile(resultFileName.c_str(), ios::out | ios::binary | ios::app);
    char* readBuffer;
    for (int i = 0; i < numOfFiles; i++){
        fstream sourceFile(fileArr[i].c_str(),ios::in | ios::binary);
        sourceFile.seekg(0,ios::end);
        long smallFileLength = sourceFile.tellg();
        sourceFile.seekg(0,ios::beg);
        readBuffer = new char[smallFileLength];

        sourceFile.read(readBuffer,smallFileLength);
        sourceFile.close();

        resultFile.write(readBuffer,smallFileLength);
    }
    resultFile.close();
    cout << "Combine Done" << endl;
}

int main() {
    string path = "";
    long long smallFileBytes;
    cout << "Enter a source file name: ";
    cin >> path;
    cout << "Enter the number of bytes in each smaller file: ";
    cin >> smallFileBytes;
    splitFile(path,smallFileBytes);

    int numOfFiles = 0;
    cout << "Enter the number of source files:";
    cin >> numOfFiles;
    string fileArr[numOfFiles];
    cout << "Enter the name of each source file:";
    for (int i = 0; i < numOfFiles; i++){
        cin >> fileArr[i];
    }
    cout << "Enter the name of merged file:";
    string resultFileName = "";
    cin >> resultFileName;
    mergeFile(numOfFiles,fileArr,resultFileName);

    return 0;
}

相关文章

  • IO流之合并流

    SequenceInputStream合并流 可将多个读取流合并,通常用于大文件切割后的合并; 文件的分割 文件的合并

  • Linux下二进制文件的分割与合并

    Linux下二进制文件的分割与合并 dd的作用是转换和拷贝文件,我们可以利用它来分割文件,相关的选项如下: if=...

  • Java File文件的分割与合并

    准备工作 定义一个定时器,监听分割文件和合并文件的耗时。 分割文件 获取需要分割文件的存储路径String sep...

  • C++ 新手必备:文件分割器

    一个小巧的VC++ 文件分割器 【源码】,将文件分割成指定块数、指定大小,然后可以合并文件,实现步骤:获得将要分割...

  • 分割和合并文件

    题目描述 假设要备份一个巨大的文件(比如10GB AVI文件)到CD-R,可以把文件分割成几个小的文件,然后逐个备...

  • Mac解压分块压缩文件

    Mac合并压缩文件7z技巧 Mac把大文件分割成小文件

  • 【Python】合并、分割PDF文件

    用到的第三方模块PyPDF2 合并给定的PDF文件 合并给定目录下的所有PDF文件 分割给定的PDF文件

  • python文件分割和合并

    https://blog.csdn.net/just_young/article/details/41487617

  • 文件分割与合并 Split

    ubuntu: 切分 切分成500M一个文件,以zxq_开头 split -b 500m zxq_生成的文件形如...

  • 文件的分割与合并

    我极限了,这个程序讲不清。我每一步都有分析,仔细看程序设计这样的程序,都是一步一步的走,感觉少什么再完善修改前面的...

网友评论

    本文标题:分割和合并文件

    本文链接:https://www.haomeiwen.com/subject/hrgnmttx.html