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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void Split(fstream &inFile, fstream &auxFileOne,fstream &auxFileTwo,int partLength)
{
string buffer;
int counter;
do
{
counter = 1;
while((counter <= partLength) && !inFile.eof())
{
getline(inFile,buffer);
auxFileOne<<buffer;
counter++;
}
counter = 1;
while((counter <= partLength) && !inFile.eof())
{
getline(inFile,buffer);
auxFileTwo<<buffer;
counter++;
}
}
while(!inFile.eof());
}
void Combine(fstream &auxFileOne,fstream &auxFileTwo,fstream &inputFile,int partLength)
{
string bufferOne;
string bufferTwo;
int counterOne;
int counterTwo;
if(!auxFileOne.eof())
getline(auxFileOne,bufferOne);
if(!auxFileTwo.eof())
getline(auxFileTwo,bufferTwo);
while(!auxFileOne.eof() && !auxFileTwo.eof())
{
counterOne = 1;
counterTwo = 1;
do
{
if(bufferOne < bufferTwo)
{
inputFile << bufferOne;
if(!auxFileOne.eof())
getline(auxFileOne,bufferOne);
counterOne++;
}
else
{
inputFile<<bufferTwo;
if(!auxFileTwo.eof())
getline(auxFileTwo,bufferTwo);
counterTwo++;
}
}
while(counterOne <= partLength && counterTwo <= partLength && !auxFileOne.eof() && !auxFileTwo.eof());
while(counterOne <= partLength && !auxFileOne.eof())
{
getline(auxFileOne,bufferOne);
inputFile << bufferOne;
counterOne++;
}
while(counterTwo <= partLength && !auxFileTwo.eof())
{
getline(auxFileTwo,bufferTwo);
inputFile<<bufferTwo;
counterTwo++;
}
}
while(!auxFileOne.eof())
{
getline(auxFileOne,bufferOne);
inputFile<<bufferOne;
}
while(!auxFileTwo.eof())
{
getline(auxFileTwo,bufferTwo);
inputFile<<bufferTwo;
}
}
int main()
{
string pathA,pathB,pathC;
fstream inputFile,auxFileOne,auxFileTwo;
int partLength = 1;
bool exitCond = false;
cout<<"Enter the path to the file" << endl;
getline(cin,pathA);
pathB=pathA.substr(0,pathA.find("."))+"_tmp001.txt";
pathC=pathA.substr(0,pathA.find("."))+"_tmp002.txt";
do
{
inputFile.open(pathA.c_str(),ios::in);
auxFileOne.open(pathB.c_str(),ios::out);
auxFileTwo.open(pathC.c_str(),ios::out);
Split(inputFile,auxFileOne,auxFileTwo,partLength);
auxFileOne.close();
auxFileTwo.close();
inputFile.close();
inputFile.open(pathA.c_str(),ios::out);
auxFileOne.open(pathB.c_str(),ios::in);
auxFileTwo.open(pathC.c_str(),ios::in);
if(!auxFileTwo.eof())
{
Combine(auxFileOne,auxFileTwo,inputFile,partLength);
partLength*=2;
}
else
exitCond = true;
auxFileTwo.close();
auxFileOne.close();
inputFile.close();
}
while(!exitCond);
return 0;
}
|