Thread: Merging files....

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    25

    Unhappy Merging files....

    Hello everyone ,
    This function is supposed to merge 2 input files both contain sorted integers from the smallest to the largest into an output file which must be sorted too … everything works fine except when I have the same number in both input files i have it written twice in the out put file and this is wrong ...I tried to solve this problem but I couldn't… any ideas????!



    Code:
    void merge ( ifstream& infile_1 , ifstream& infile_2 , ofstream&outfile )
     
    {
    
      int number_1 , number_2  ;
      
      infile_1 >> number_1 ;
      infile_2 >> number_2 ; 
    
      while ( (!infile_1.eof()) &&(!infile_2.eof()) )
     // while not end of file for both input files...
       
      { 
    	  if ( number_1 > number_2)
    	  { 
    	      outfile << number_2 << endl ;
    	      infile_2 >> number_2 ;
    	  }	
         
      // ====================================//
    
          else if ( number_1 < number_2)
                     {
    	      outfile << number_1 << endl ;
    	      infile_1 >> number_1 ;
    	  }
    
      // ====================================//
    
          if ( number_1 == number_2)
                  {
                      outfile << number_1 << endl ;
    	  infile_1 >> number_1 ;
                  }
       }
    
      // ****************************************//
    
      while (!infile_1.eof()) 
     // while there are still numbers in the first input file...
        
       {  
           outfile << number_1 << endl ;
            infile_1 >> number_1 ;
       }
    	 
    
      while (!infile_2.eof()) 
     // while there are still numbers in the second input file...
       
      {  
            outfile << number_2 << endl ;
            infile_2 >> number_2 ;
      }
    
      
    }
    have a nice day

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    25

    Unhappy Doesn't work .....

    Nope it didn't work....
    have a nice day

  3. #3
    Try this (I added in comments where i changed it)
    Code:
    ...
    while ( (!infile_1.eof()) &&(!infile_2.eof()) )
     // while not end of file for both input files...
       
      { 
    	  if ( number_1 > number_2)
    	  { 
    	      outfile << number_2 << endl ;
    	      infile_2 >> number_2 ;
    	  }	
         
      // ====================================//
    
      else if ( number_1 < number_2)
      {
        outfile << number_1 << endl ;
        infile_1 >> number_1 ;
      }
    
      // ====================================//
    
      else if ( number_1 == number_2) // Yes, this should be an else.  If one of the above matches, then this could trigger also, which would produce errors.  You only want one of these three to trigger
      {
          outfile << number_1 << endl ;
        infile_1 >> number_1 ;
        infile_2 >> number_2 ;  // You need to get rid of BOTH numbers, because they are equal,  and you only want one of them.
      }
       
    ...
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    25

    Talking

    It worked thanx
    have a nice day

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  2. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 08:36 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM