title: Order files with MergeSort in c++
body: <p>I'm triying to implement my own MergeSort, but I've got some problems, see if anyone can help me a little.</p>
  
  <p>I have a big file with some info separeted with coma (Name,city,mail,telf). I would like to apply mergesort to order it, because I supose that the client computer wont have as much memory to do it in one try.</p>
  
  <p>So, I split it into files of MAX_CUSTOMERS lines, and order them individually, all correct until here, but when I want to get the first two files and order them, I've got all the problems, I got repeated, ones and others dissapear, here's my code:</p>
  
  <pre><code>void MergeSort(string file1Name, string file2Name,string name){
  printf("Enter MERGE SORT %s AND %s\n",file1Name.c_str(),file2Name.c_str());
  string temp;
  string fileName;
  string lineFile1, lineFile2;
  bool endFil1 = false, endFil2 = false;
  int numCust1 = 0;
  int numCust2 = 0;
  int x1 = 0, x2 = 0;
  
  ifstream file1;
  file1.open(file1Name.c_str());
  ifstream file2;
  file2.open(file2Name.c_str());
  
  ofstream mergeFile; 
  fileName = "customers_" +name +".txt";
  cout &lt;&lt; "Result file " &lt;&lt; fileName &lt;&lt; endl;
  mergeFile.open("temp.txt");
  
  getline(file1,lineFile1);
  getline(file2,lineFile2);
  
  while(!endFil1 &amp;&amp; !endFil2){
      if(CompareTelf(lineFile1,lineFile2)==1){
          mergeFile &lt;&lt; lineFile1 &lt;&lt; endl;
          if(!getline(file1,lineFile1)){
              cout &lt;&lt; lineFile1 &lt;&lt; endl;
              cout &lt;&lt; "1st file end" &lt;&lt; endl;         
              endFil1 = true;
          }
      }else{
          mergeFile &lt;&lt; lineFile2 &lt;&lt; endl;
          if(!getline(file2,lineFile2)){
              cout &lt;&lt; lineFile2 &lt;&lt; endl;
              cout &lt;&lt; "2nd file end" &lt;&lt; endl;         
              endFil2 = true;
          }
      }       
  }
  if(endFil1){
      //mergeFile &lt;&lt; lineFile2 &lt;&lt; endl;
      while(getline(file2,lineFile2)){
          mergeFile &lt;&lt; lineFile2 &lt;&lt; endl;
      }
  }else{
      //mergeFile &lt;&lt; lineFile1 &lt;&lt; endl;
      while(getline(file1,lineFile1)){
          mergeFile &lt;&lt; lineFile1 &lt;&lt; endl;
      }
  }
  
  file1.close();
  file2.close();
  mergeFile.close();
  rename("temp.txt",fileName.c_str());
  return;
  }
  
  Customer SplitLine(string line){
  string splitLine;
  string temp;
  Customer cust;
  int actProp = 0;
  int number;
  istringstream readLineStream(line); //convert String readLine to Stream readLine
  
  while(getline(readLineStream,splitLine,',')){
      if (actProp == 0)cust.name = splitLine;
      else if (actProp == 1)cust.city = splitLine;
      else if (actProp == 2)cust.mail = splitLine;
      else if (actProp == 3)cust.telf = atoi(splitLine.c_str());
      actProp++;
  }
  //printf("Customer read: %s, %s, %s, %i\n",cust.name.c_str(), cust.city.c_str(), cust.mail.c_str(), cust.telf);
  
  return cust;
  }
  
  int CompareTelf(string str1, string str2){
      Customer c1 = SplitLine(str1);
      Customer c2 = SplitLine(str2);
  
      if(c1.telf&lt;c2.telf)return 1; //return 1 if 1st string its more important than second, otherwise, return -1
      else return -1;
  }
  
  struct Customer{
          string name;
          string city;
          string mail;
          long telf;
  };
  </code></pre>
  
  <p>If have some question about the code, just say it! I tried to use varNames as descriptive as possible!</p>
  
  <p>Thanks a lot.</p>