Splitting files and regrouping splitted files

Hello again,

I'm trying to split a file in chunks, and regrouping the chunks later, but i don't know what is going wrong.

- The main function splits a file into "n" parts of "nChunkSize" size
- The regroup function is supposed to write the chunks into one file to rebuild it

What is "almost" working :

- The chunk files are successfully created, having the same size excepted the last one
- The rebuilt final file has "almost" the same size as the initial one

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <sstream>

using namespace std;

int regroup(int nParts);

int main(int argc, char * argv[])
{   //variables
    std::ostringstream sStringer;
    ifstream::pos_type nSize;
    ifstream fSource(argv[1], ios_base::ate|ios::binary|ios::in);
    char * sMemBlock;
    nSize = fSource.tellg();
    string sExtension = strstr(argv[1],".");
    int nGetPointer = 0;
    string sChunkSize = argv[2];
    istringstream nIntegerer(sChunkSize);
    int nChunkSize;
    nIntegerer >> nChunkSize;
    int nLastChunkSize = nChunkSize;
    int nPartNumber = 1;
    string sDestinationFile;

    if (fSource.is_open())
    {
        cout << "File input : " << argv[1] << endl;
        cout << "Size : " << nSize << " bytes"<< endl;
        cout << "Extension : " << sExtension << endl <<endl;

        fSource.seekg(0, ios::beg);

        while (fSource.tellg() < nSize)
        {
            fSource.seekg(nGetPointer, ios::beg);

            if (nGetPointer + nChunkSize > nSize)
            {
                while (nGetPointer + nLastChunkSize > nSize)
                {
                    nLastChunkSize--;
                }
                sMemBlock = new char[nLastChunkSize];
                fSource.read(sMemBlock, nLastChunkSize);
            }
            else
            {
                sMemBlock = new char[nChunkSize];
                fSource.read(sMemBlock, nChunkSize);
            }

            sDestinationFile = argv[1];
            sDestinationFile.append(".part");
            sStringer.str("");
            sStringer << nPartNumber;
            sDestinationFile.append(sStringer.str());

            cout << "Destination file : " << sDestinationFile << endl;
            cout << "Chunk Size : " << nLastChunkSize << endl;
            ofstream fDestination(sDestinationFile.c_str());
            fDestination.write(sMemBlock, nLastChunkSize);

            nGetPointer += nChunkSize;
            nPartNumber += 1;
        }
        regroup(nPartNumber-1);
    }
    return 0;
}

int regroup(int nParts)
{
    string sChunkFile;
    std::ostringstream sStringer;
    int nPartNumber = 1;
    char * sMemBlock;
    ifstream::pos_type nSize;

    ofstream fRetour("recup.avi");

    for (nPartNumber = 1; nPartNumber <= nParts; nPartNumber++)
    {
        sChunkFile = "episode.avi";
        sChunkFile.append(".part");
        sStringer.str("");
        sStringer << nPartNumber;
        sChunkFile.append(sStringer.str());
        ifstream fChunk(sChunkFile.c_str(), ios::in|ios::binary|ios::ate);
        nSize = fChunk.tellg();
        sMemBlock = new char[nSize];
        fChunk.seekg(0, ios::beg);
        fChunk.read(sMemBlock, nSize);
        fChunk.close();
        fRetour.write(sMemBlock, nSize);
    }
    fRetour.close();
    return 0;
}


In the generated regrouped file, i can see with a hex editor that some bytes are skipped. Because of this, the final regrouped file is corrupted.

I am wondering what should I change to get it to work?

Have a good day :-)
You do realise that you can split files with split and put them back together with cat (or copy /b on Windows).

nLastChunkSize = size mod nChunkSize. No need for that inner while loop that spins.

Also, number of files you produced is:
size / nChunkSize full length files + 1 partially filled file if nLastChunkSize is not zero.

If you declared your variables where they're used, you would realise that you leak a buffer nChunkSize big each time you loop.

You don't need to seek, get the file info from the file metadata using stat instead.

The real problem is:
while (fSource.tellg() < nSize)
should be:
while (fSource)
I expect it's not terminating when you expect it to.
Topic archived. No new replies allowed.