Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File Merging in C++
#4
This is getting way messy.

So I'm trying to sort the numbers now. However, what I came up with after trying to write an algorithm out of it was a mess of if-else statements within a do-while. Problem with this is that it... mostly works, except it only compares the two next files that gets read, without comparing them to the ones before them (meaning that while, for instance, 12 in file A and 10 in file B are compared and get sorted into 10 and 12 in the output, there could have been an 11 in file A that was already checked before and that ends up becoming 11, 10, 12).

I'm sure there's a less messy way to do this...

Code:
do
{
    ReadValue (inF1, num1, success1);            //Read the value of the first file.
    if(success1)
    {
        ReadValue (inF2, num2, success2);        //Read the value of the second file if the first was successfully read.
        if(success2)                            
        {
            if(num1 < num2)                //Check if the value of the first file read is greater than the value of the second file read.
            {                                    
                WriteValue (outF, num1);    //Place first file's value before the second.
                WriteValue (outF, num2);    //Place second file's value after the first.
            }
            else
            {
                WriteValue (outF, num2);    //Place second file's value before the first.
                WriteValue (outF, num1);    //Place first file's value after the second.
            }
        }
        else
        {
            WriteValue (outF, num1);        //If nothing in the second file is read, print only the first file's read value.
        }
    }
    else
    {
        ReadValue(inF2, num2, success2);        //Read the second file's value if no value was read in the first.
        if(success2)
        {
            WriteValue(outF, num2);            //If a value is read in the second file, print only the second file's read value.
        }
    }
}
while (success1 || success2);                    //Run loop while success is true.

Any suggestions for a simpler fix? @_@
Reply


Messages In This Thread
File Merging in C++ - by MariettaRC - 2013-09-27, 06:37 AM
File Merging in C++ - by SaptaZapta - 2013-09-27, 07:41 AM
File Merging in C++ - by MariettaRC - 2013-09-27, 07:59 AM
File Merging in C++ - by MariettaRC - 2013-09-27, 11:38 AM
File Merging in C++ - by SaptaZapta - 2013-09-27, 11:43 AM
File Merging in C++ - by MariettaRC - 2013-09-27, 11:53 AM
File Merging in C++ - by Dusk - 2013-09-27, 11:55 AM
File Merging in C++ - by SaptaZapta - 2013-09-27, 12:07 PM
File Merging in C++ - by MariettaRC - 2013-09-27, 01:52 PM
File Merging in C++ - by SaptaZapta - 2013-09-27, 02:00 PM
File Merging in C++ - by MariettaRC - 2013-09-27, 02:02 PM
File Merging in C++ - by MariettaRC - 2013-09-28, 02:31 PM
File Merging in C++ - by SaptaZapta - 2013-09-28, 03:23 PM
File Merging in C++ - by Dusk - 2013-09-28, 03:23 PM
File Merging in C++ - by MariettaRC - 2013-09-28, 03:30 PM
File Merging in C++ - by SaptaZapta - 2013-09-28, 03:38 PM
File Merging in C++ - by MariettaRC - 2013-09-28, 03:47 PM
File Merging in C++ - by Dusk - 2013-09-28, 03:51 PM
File Merging in C++ - by SaptaZapta - 2013-09-28, 03:56 PM
File Merging in C++ - by MariettaRC - 2013-09-28, 04:02 PM
File Merging in C++ - by SaptaZapta - 2013-09-28, 04:13 PM

Forum Jump:


Users browsing this thread: