#include #include #include #include #include #include #include #include #include #include #include using namespace std ; string dataFileName ; int indexG = 1 ; const int blockSize = 2 ; ifstream dataFile ; char ch1 ; int countNoLines( string filename ) { ifstream inputFile(filename); // Open the file for reading if (!inputFile.is_open()) { // Check if the file was opened successfully cerr << "Error: Could not open the file \"" << filename << "\"" << endl; return 1; // Indicate an error } int lineCount = 0; string line; // Read the file line by line until the end of the file is reached while (getline(inputFile, line)) { if ( line.length() > 0 && line[0] != 10 && line[0] != 13 ) { lineCount++; // Increment the line counter for each line read cout << "Line:" << line.length() << " " << (int)line[0] << endl ; } } inputFile.close(); return lineCount ; } void readIntoArray( int tempArr1[], int size ) { static bool hasBeenOpened = false ; cout << " readIntoArray: Step1" << endl ; if ( !hasBeenOpened ) { dataFile.open( dataFileName ) ; hasBeenOpened = true ; } int x1 ; cout << "readIntoArray size: " << size << endl ; for( int i1=0 ; i1> x1 ; cout << "x1:" << x1 << endl ; tempArr1[i1] = x1 ; } //for } void writeArrayIntoFile( int tempArr1[], int size , string fileName ) { ofstream outputFile ; outputFile.open( fileName ); for( int i1=0 ; i1 < size ; i1++ ) { outputFile << tempArr1[i1] << endl ; } outputFile.close() ; } void printArray( int arr1[] , int size ) { cout << "Printing the array1." << endl ; for( int i1=0 ; i1> ch1 ; } //while //if ( file1InputStream.eof() ) if ( file1IsDone ) { cout << "Reached end of file1." << endl ; outputFileStream << x2 << endl ; while( getline( file2InputStream, line ) ) { cout << "line 3." << line << endl ; if ( line.length() > 0 ) { x2 = stoi(line) ; outputFileStream << x2 << endl ; } } //while } else { cout << "Reached end of file2." << endl ; outputFileStream << x1 << endl ; while( getline( file1InputStream, line ) ) { cout << "line 4." << line << endl ; if ( line.length() > 0 ) { x1 = stoi(line) ; outputFileStream << x1 << endl ; } } //while } file1InputStream.close() ; file2InputStream.close() ; outputFileStream.close() ; } void mergeSort( int start , int end , string outputFile ) { cout << "mergeSort: " << start << " " << end << endl ; if ( (end-start) < blockSize ) { cout << "Working on block. " << start << " " << end << ":outputFile: " << outputFile << endl ; int sizeOfArray = (end-start) + 1 ; int tempArr1[ sizeOfArray ] ; readIntoArray( tempArr1 , sizeOfArray) ; sort( tempArr1 , (tempArr1 + sizeOfArray) ) ; printArray( tempArr1 , sizeOfArray ) ; writeArrayIntoFile(tempArr1, sizeOfArray , outputFile) ; } else { int middle = start + ( end - start ) / 2 ; string file1 = "file" + to_string(indexG++) + ".txt" ; string file2 = "file" + to_string(indexG++) + ".txt" ; cout << "Splitting the file. middle:" << middle << endl ; mergeSort( start , middle , file1 ) ; mergeSort( middle+1 , end , file2 ) ; merge( file1 , file2 , outputFile ) ; } } int main() { cout << "Enter the name of input file: " << endl ; //cin >> dataFileName ; dataFileName = "input.txt" ; int noOfLines = countNoLines( dataFileName ) ; cout << "noOfLines: " << noOfLines << endl ; mergeSort( 0 , noOfLines-1, "result.txt" ) ; // merge( "file1.txt" , "file2.txt" , "outputFile.txt" ) ; return 0; }