Advertisement
Guest User

Untitled

a guest
Sep 9th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bool mergeOfl(ifstream &Din, string &Id, int &Experience, string &Married, float &Wage, string &Industry)
  2. {
  3.  
  4.     string ipath = FILENAME + "input.txt";
  5.     string path = FILENAME + "overflow.txt";
  6.     string tpath = FILENAME + "temp.txt";
  7.  
  8.     string Id2 = "ID";
  9.     int Experience2 = -1;
  10.     string Married2 = "UNKNOWN";
  11.     float Wage2 = -1.0;
  12.     string Industry2 = "INDUSTRY";
  13.  
  14.     int temp;
  15.     string msid;
  16.  
  17.     bool found = true;
  18.  
  19.     ofstream outfile(tpath.c_str());
  20.     outfile.close();
  21.     ifstream Din2(path.c_str());
  22.  
  23.     NUM_RECORDS += OFL_RECORDS;
  24.  
  25.     cout << "** Merging database subsections." << endl;
  26.  
  27.     headLine(tpath);
  28.  
  29.  
  30.     //get the last record ID of the sorted section, ie the largest
  31.     Din.seekg(-69, ios::end);
  32.     Din >> msid;
  33.     Din.seekg(71, ios::beg);
  34.     Din2.seekg(0, ios::beg);
  35.  
  36.     cout << "** Largest sorted ID is: " << msid << endl;
  37.  
  38.     temp = atoi(msid.c_str());
  39.  
  40.     //cycle through the IDs from 1 to largest ID
  41.     //if it doesnt exist in the sorted section, check the unsorted section
  42.     for(int x = 1; x <= temp; x++)
  43.     {
  44.         //if we found the last ID, the sorted section can check the next line
  45.         if(found == true)
  46.         {
  47.             Din >> Id >> Experience >> Married >> Wage >> Industry;
  48.             //check sorted section
  49.             if(atoi(Id.c_str()) == x)
  50.             {
  51.                 if(Experience != -1)
  52.                 {
  53.                     appLine(Id, Experience, Married, Wage, Industry, tpath);
  54.                 }
  55.                 else
  56.                 {
  57.                     NUM_RECORDS--;
  58.                 }
  59.                 found = true;
  60.             }
  61.             //check unsorted section
  62.             else
  63.             {
  64.                 stringstream ss;
  65.                 ss << x;
  66.                 string tempid = modID(ss.str());
  67.  
  68.  
  69.                 if(linearSearch(Din2, tempid, Experience2, Married2, Wage2, Industry2) == true)
  70.                 {
  71.                     appLine(tempid, Experience2, Married2, Wage2, Industry2, tpath);
  72.                     //need to make sure we dont try to merge this record again
  73.                     updateRecord(Din2, tempid, Experience2, Married2, Wage2, Industry2, 1, "-1");
  74.                 }
  75.                     found = false;
  76.  
  77.             }
  78.         }
  79.         //if we didnt find the last ID, we need to stay on the current line in the sorted section
  80.         else
  81.         {
  82.             //check sorted section
  83.             if(atoi(Id.c_str()) == x)
  84.             {
  85.                 if(Experience != -1)
  86.                 {
  87.                     appLine(Id, Experience, Married, Wage, Industry, tpath);
  88.                 }
  89.                 else
  90.                 {
  91.                     NUM_RECORDS--;
  92.                 }
  93.                 found = true;
  94.             }
  95.             //check unsorted section
  96.             else
  97.             {
  98.                 stringstream ss;
  99.                 ss << x;
  100.                 string tempid = modID(ss.str());
  101.  
  102.  
  103.                 if(linearSearch(Din2, tempid, Experience2, Married2, Wage2, Industry2) == true)
  104.                 {
  105.                     appLine(tempid, Experience2, Married2, Wage2, Industry2, tpath);
  106.                     //need to make sure we dont try to merge this record again
  107.                     updateRecord(Din2, tempid, Experience2, Married2, Wage2, Industry2, 1, "-1");
  108.                 }
  109.  
  110.                 found = false;
  111.             }
  112.         }
  113.  
  114.     }
  115.  
  116.     Din.close();
  117.     Din2.close();
  118.     Din2.open(path.c_str());
  119.  
  120.     string stemp;
  121.     //int count = OFL_RECORDS - 1;
  122.  
  123.     //now we need to cycle through the rest of the unsorted section
  124.     //and merge any unused records, starting with the smallest
  125.     for(int x = 0; x < OFL_RECORDS; x++)
  126.     {
  127.         Din2.seekg(x * RECORD_SIZE, ios::beg);
  128.         //cout << Din2.tellg() << endl;
  129.  
  130.         //this is where it breaks
  131.         //din2 fails to pull in any new data, so ID is still the
  132.         //last id from the sorted section
  133.         Din2 >> Id >> Experience >> Married >> Wage >> Industry;
  134.         if(Experience == -1)
  135.         {
  136.             continue;
  137.         }
  138.         else
  139.         {
  140.             //compare selected ID to every other ID...
  141.             Din2.seekg(0, ios::beg);
  142.             for(int y = 0; y < OFL_RECORDS; y++)
  143.             {
  144.                 //except for our own
  145.                 if(y != x)
  146.                 {
  147.                     Din2 >> Id2 >> Experience2 >> Married2 >> Wage2 >> Industry2;
  148.                     //if the record has been deleted or already used, skip it
  149.                     if(Experience2 == -1)
  150.                     {
  151.                         continue;
  152.                     }
  153.                     //elsewise compare it and choose the smaller ID record
  154.                     else
  155.                     {
  156.                         if(atoi(Id2.c_str()) < atoi(Id.c_str()) && Experience2 != -1)
  157.                         {
  158.                             Id = Id2;
  159.                             Experience = Experience2;
  160.                             Married = Married2;
  161.                             Wage = Wage2;
  162.                             Industry = Industry2;
  163.                         }
  164.                     }
  165.  
  166.  
  167.                     appLine(Id, Experience, Married, Wage, Industry, tpath);
  168.                     //make sure we dont use the chosen record again
  169.                     updateRecord(Din, Id, Experience, Married, Wage, Industry, 1, "-1");
  170.                 }
  171.                 //skipping our own ID
  172.                 else
  173.                 {
  174.                     Din2 >> Id2 >> Experience2 >> Married2 >> Wage2 >> Industry2;
  175.                 }
  176.             }
  177.         }
  178.     }
  179.  
  180.     Din2.close();
  181.  
  182.     //remove(ipath.c_str());
  183.     //remove(path.c_str());
  184.     //rename(tpath.c_str(), ipath.c_str());
  185.  
  186.     //reset the number of unsorted records
  187.     OFL_RECORDS = 0;
  188.  
  189.     return true;
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement