Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default File Merging in C++


    Expect many of these threads from me until the end of the semester.

    Basic premise of this assignment: Have two input files containing numbers, merge them into one output file with the numbers ordered, 8 values per line. Program needs to also print out the sum and amount of values of each separate file, as well as the combined file.

    I got as far as having it list the sum and amount of values of the two input files. My problem right now is getting them to "merge." I'm doing this one step at a time, so I'm just getting them to merge right now.

    First I tried to get one input file to print out its values on an output file on a separate project (thanks to @SaptaZapta for suggesting this) to see if I got the basic concept down. This worked fine.

    So now I tried to apply this to the assignment... and it won't work. At one point, I tested this with just one file, and it worked. Then I tried with the second file alone, and the output file comes out blank. Tested with the first file again... and it comes out blank?!

    http://pastebin.com/NDyR2dWx

    I'm just not understanding what's going on here.

    Note: I do delete the output file after every test run, so a new output file is always made whenever I test it.
    Note 2: I figured that if I could get just one file to print into an output file, for both files, I could see if both will work at the same time by giving each their own separate num and success calls (num1 and success1 for the first file, num2 and success2 for the second), but I can't very well test that, now can I (I did try). ;_;

  2. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 225
    Job: Hero
    Guild: Matriarchy
    Alliance: Dominion

    Default Re: File Merging in C++


    Two possibilities I see: one is that the second input file is somehow corrupted and nothing gets read (program outputs 0 numbers) or that the output file is being buffered and because you never close() it explicitely it doesn't get written to disc.

    To see what's going on, add debug messages, like:
    Code:
    ReadValue(inF, num, success);
    cout << "read number " << num << endl;
    WriteValue(inF, num);
    cout <<  "wrote number " << num << endl;

  3. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: File Merging in C++


    EDIT:

    Okay, I added outF.close(); and it worked!

    ... Now to figure out why it's reading values more than once.

    nvm got that too

  4. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: File Merging in C++


    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? @_@

  5. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 225
    Job: Hero
    Guild: Matriarchy
    Alliance: Dominion

    Default Re: File Merging in C++


    I'll give you a hint: the doctor can only see one person at a time. Just because you've seen them both doesn't mean they absolutely have to be the next two people in.

    In your example, how did the 11 get to go in before the 10?

  6. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: File Merging in C++


    I was thinking along the lines of that... but I would have to add yet another if statement. I GUESS I'LL DO THAT unless that's not the answer. (It probably isn't.)

    ... I'll be scribbling in my notebook some more then.

    Also,

    File A has the numbers 2 4 5 7 7 9 11 12.
    File B has the numbers 3 4 6 6 7 8 9 10 13 15.

    It checks 2 at a time, usually. It gets to 11 and 9, checks those, places 9 before 11. Then it moves on to 12 and 10. 10 is smaller than 12, so it gets placed accordingly. It ends up with 9, 11, 10, 12.

  7. Default Re: File Merging in C++


    It's a lot easier if you approach the problem by writing an algorithm to add a single number to the right place in your array.

    Then you can just read through both files and call the function on each number you read.

  8. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 225
    Job: Hero
    Guild: Matriarchy
    Alliance: Dominion

    Default Re: File Merging in C++


    That's exactly it. The way you should do it is, you see 11 and 9, you tell 9 "go ahead" and you hold 11 back. You do not send him on before you see who was standing after the 9 (and is now top of file A).

  9. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: File Merging in C++


    Code:
    do
    {
    	ReadValue (inFA, A, successA);			//Read the value of file A.
    	if(successA)
    	{
    		ReadValue (inFB, B, successB);		//Read the value of file B if the first was successfully read.
    		if(successB)							
    		{
    			//I WANT TO LOOP HERE.
    			if(A < B)				//Check if the value of file A read is greater than the value of file B.
    			{									
    				WriteValue (outF, A);		//Write the value of file A if smaller than that of file B.
    				ReadValue (inFA, A, successA);	//Read next value in file A.
    			}
    			else
    			{
    				WriteValue (outF, B);		//Write the value of file B if smaller than that of file A.
    				ReadValue (inFB, B, successB);	//Read next value in file B.
    			}
    			//END LOOP HERE IF ONE OF THE SUCCESSES FAILS.
    		}
    		else
    		{
    			WriteValue (outF, A);			//If nothing in file B is read, print only file A's value.
    		}
    	}
    	else
    	{
    		ReadValue (inFB, B, successB);
    		if(successB)
    		{
    			WriteValue(outF, B);
    		}
    	}
    }while (successA || successB);
    Problem here: If I place a while loop containing (successA && success B) in the area I want to place a loop in, I get an infinite loop. But it shouldn't do that if one of the functions eventually fails reading a value... right?
    Last edited by MariettaRC; 2013-09-27 at 01:58 PM. Reason: Forgot an else at the bottom.

  10. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 225
    Job: Hero
    Guild: Matriarchy
    Alliance: Dominion

    Default Re: File Merging in C++


    Right, the loop should not be infinite.
    If it seems to be running forever, try making WriteValue output the numbers to cout (as well as outF) so you can see how the program is progressing and where it's bogging down.

  11. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: File Merging in C++


    Yeah, I made a small fix and it's mostly right now, but now it's skipping a number.

    I'll do some debugging and see what's up. ;_;

  12. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: File Merging in C++


    UNIX's repository is picky as hell jesus christ

    Program is working. As far as I could tell, I did everything the assignment asked for. But using system("pause"); was a bad idea since it's not portable. I used cin.get(); instead, but now my program closes anyway. What's a working portable equivalent to system("pause")?

    EDIT: Okay... it took that regardless. Whatever, I'll take it.

    Also, this is one of the results on the repository...

    "... check for ''inF.clear();+inF.seekg(+0L,+ios+::+beg+);'' ==> NOT FOUND"

    Uh... but in my code I...

    Code:
    int Sum (istream & inF)
    {
    	inF.clear();
    	inF.seekg(0L, ios::beg);  <--- IT'S RIGHT THERE.
     
    	int sum = 0;
    	int num;
    	while (inF >> num)
    	{
    		sum += num;
    	}
    	return sum;
    }
    ????
    Last edited by MariettaRC; 2013-09-28 at 03:31 PM. Reason: nvm that other one went away for some reason

  13. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 225
    Job: Hero
    Guild: Matriarchy
    Alliance: Dominion

    Default Re: File Merging in C++


    That's not Unix. That's whatever software your teacher is using to check your code. You'll have to ask the teacher, a TA, or maybe your fellow students that.


  14. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: File Merging in C++


    Right, it's a repository. Didn't know what else to call it until now. :I I've e-mailed my professor about that, hope he responds before the due time tonight.

    It checks to see if I've included certain things in the program, basically.

    Also, it gave me a warning saying "Warning: No newline at end of file." What am I supposed to do here? Google isn't exactly helping.
    Last edited by MariettaRC; 2013-09-28 at 03:43 PM. Reason: Typo.

  15. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 225
    Job: Hero
    Guild: Matriarchy
    Alliance: Dominion

    Default Re: File Merging in C++


    "New newline" or "no newline"?
    If it's no newline, just go in the text editor, go to the bottom of the file, and add a newline.
    Basically it wants an empty line (not even blank or tab) at the end of the file.

  16. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: File Merging in C++


    Whoops, typo'd that. No newline.

    So...

    Code:
    int main()
    {
    	...
    
    	//-| ----------------------------------------------------------------------
    	//-| Print the copyright notice declaring authorship again.
    	//-| ----------------------------------------------------------------------
    	cout << endl << "(c) 2013, yourUnixLogin First Last" << endl << endl; 
    
    
       return 0;
    
    }//main
    That has two endl commands, I guess that doesn't count? (copy/pasted from the code's shell.)

  17. Default Re: File Merging in C++


    I think it means at the end of your code file.

  18. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 225
    Job: Hero
    Guild: Matriarchy
    Alliance: Dominion

    Default Re: File Merging in C++


    Yes, it means at the end of your source file. Make sure there's an empty line under the "} // main" line.

  19. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: File Merging in C++


    All right, gotcha. Thanks so much guys, I do believe I'm finally done with this. 23/25... not perfect, but still good. Maybe I can get it closer to perfect if my professor e-mails me before submissions close, but if not, oh well. As long as it gives me enough of a chance to pass the class altogether. ;_;

  20.  

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •