Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool mergeOfl(ifstream &Din, string &Id, int &Experience, string &Married, float &Wage, string &Industry)
- {
- string ipath = FILENAME + "input.txt";
- string path = FILENAME + "overflow.txt";
- string tpath = FILENAME + "temp.txt";
- string Id2 = "ID";
- int Experience2 = -1;
- string Married2 = "UNKNOWN";
- float Wage2 = -1.0;
- string Industry2 = "INDUSTRY";
- int temp;
- string msid;
- bool found = true;
- ofstream outfile(tpath.c_str());
- outfile.close();
- ifstream Din2(path.c_str());
- NUM_RECORDS += OFL_RECORDS;
- cout << "** Merging database subsections." << endl;
- headLine(tpath);
- //get the last record ID of the sorted section, ie the largest
- Din.seekg(-69, ios::end);
- Din >> msid;
- Din.seekg(71, ios::beg);
- Din2.seekg(0, ios::beg);
- cout << "** Largest sorted ID is: " << msid << endl;
- temp = atoi(msid.c_str());
- //cycle through the IDs from 1 to largest ID
- //if it doesnt exist in the sorted section, check the unsorted section
- for(int x = 1; x <= temp; x++)
- {
- //if we found the last ID, the sorted section can check the next line
- if(found == true)
- {
- Din >> Id >> Experience >> Married >> Wage >> Industry;
- //check sorted section
- if(atoi(Id.c_str()) == x)
- {
- if(Experience != -1)
- {
- appLine(Id, Experience, Married, Wage, Industry, tpath);
- }
- else
- {
- NUM_RECORDS--;
- }
- found = true;
- }
- //check unsorted section
- else
- {
- stringstream ss;
- ss << x;
- string tempid = modID(ss.str());
- if(linearSearch(Din2, tempid, Experience2, Married2, Wage2, Industry2) == true)
- {
- appLine(tempid, Experience2, Married2, Wage2, Industry2, tpath);
- //need to make sure we dont try to merge this record again
- updateRecord(Din2, tempid, Experience2, Married2, Wage2, Industry2, 1, "-1");
- }
- found = false;
- }
- }
- //if we didnt find the last ID, we need to stay on the current line in the sorted section
- else
- {
- //check sorted section
- if(atoi(Id.c_str()) == x)
- {
- if(Experience != -1)
- {
- appLine(Id, Experience, Married, Wage, Industry, tpath);
- }
- else
- {
- NUM_RECORDS--;
- }
- found = true;
- }
- //check unsorted section
- else
- {
- stringstream ss;
- ss << x;
- string tempid = modID(ss.str());
- if(linearSearch(Din2, tempid, Experience2, Married2, Wage2, Industry2) == true)
- {
- appLine(tempid, Experience2, Married2, Wage2, Industry2, tpath);
- //need to make sure we dont try to merge this record again
- updateRecord(Din2, tempid, Experience2, Married2, Wage2, Industry2, 1, "-1");
- }
- found = false;
- }
- }
- }
- Din.close();
- Din2.close();
- Din2.open(path.c_str());
- string stemp;
- //int count = OFL_RECORDS - 1;
- //now we need to cycle through the rest of the unsorted section
- //and merge any unused records, starting with the smallest
- for(int x = 0; x < OFL_RECORDS; x++)
- {
- Din2.seekg(x * RECORD_SIZE, ios::beg);
- //cout << Din2.tellg() << endl;
- //this is where it breaks
- //din2 fails to pull in any new data, so ID is still the
- //last id from the sorted section
- Din2 >> Id >> Experience >> Married >> Wage >> Industry;
- if(Experience == -1)
- {
- continue;
- }
- else
- {
- //compare selected ID to every other ID...
- Din2.seekg(0, ios::beg);
- for(int y = 0; y < OFL_RECORDS; y++)
- {
- //except for our own
- if(y != x)
- {
- Din2 >> Id2 >> Experience2 >> Married2 >> Wage2 >> Industry2;
- //if the record has been deleted or already used, skip it
- if(Experience2 == -1)
- {
- continue;
- }
- //elsewise compare it and choose the smaller ID record
- else
- {
- if(atoi(Id2.c_str()) < atoi(Id.c_str()) && Experience2 != -1)
- {
- Id = Id2;
- Experience = Experience2;
- Married = Married2;
- Wage = Wage2;
- Industry = Industry2;
- }
- }
- appLine(Id, Experience, Married, Wage, Industry, tpath);
- //make sure we dont use the chosen record again
- updateRecord(Din, Id, Experience, Married, Wage, Industry, 1, "-1");
- }
- //skipping our own ID
- else
- {
- Din2 >> Id2 >> Experience2 >> Married2 >> Wage2 >> Industry2;
- }
- }
- }
- }
- Din2.close();
- //remove(ipath.c_str());
- //remove(path.c_str());
- //rename(tpath.c_str(), ipath.c_str());
- //reset the number of unsorted records
- OFL_RECORDS = 0;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement