Thread: How do i sort that file after merge

  1. #1

    How do i sort that file after merge

    Hi im a beginner programmer. Im trying to put two files together into a third file and have it sorted. I have success on putting the two files together but they still wont get sorted even though I have the if else statements in there. Can someone tell me what im doing wrong because i dont. here is my code....
    	#include <stdio.h>
    #include <limits.h>
    #define READ_MODE "rb"
    #define WRITE_MODE "wb"
    int main (void)
    	FILE *fpM1;
    	FILE *fpM2;
    	FILE *fpOut;
    	int recM1;
    	int recM2;
    	char file1ID[]   = "P13Mrg1.bin";
    	char file2ID[]   = "P13Mrg2.bin";
    	char fileOutID[] = "P13Mrg3.bin";
    	int sentinel     = INT_MAX;
    	int mergeCnt     = 0;
    	printf("begin File Merge:\n");
    	if(!(fpM1 = fopen (file1ID, READ_MODE)))
    	printf("\aError on %s\n",file1ID);
    	if(!(fpM2 = fopen (file2ID, READ_MODE)))
    	printf("\aError on %s\n",file2ID);
    	if(!(fpOut = fopen (fileOutID, WRITE_MODE)))
    	printf("\aError on %s\n",fileOutID);
    	fread (&recM1, sizeof(int),1,fpM1);
    	if (feof(fpM1))
    		recM1 = sentinel;
    	fread (&recM2, sizeof(int),1,fpM2);
    	if (feof(fpM2))
    		recM2 = sentinel;
    	while(!feof(fpM1) || !feof(fpM2))
    		if(recM1<= recM2)
    			fread (&recM1,sizeof(int),1,fpM1);
    			if (feof(fpM1))
    			fwrite(&recM2, sizeof(int),1,fpOut);
    			if (feof(fpM2))
    				recM2 = sentinel;
    	fclose (fpM1);
    	fclose (fpOut);
    printf("End File Merge. %d items merged.\n",mergeCnt);
    return 0;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Can you be more specific about what is going wrong?

    On the face of it, it doesn't look too far off working

    You're sure that you wrote your ints to your input files using fwrite and not fprintf

  3. #3


    yeah im pretty sure that everything is okay with this code syntax wise. The program should result in the third file being sorted from smallest number to largest number.... can anyone try it, ive asked like 10 people already and no one understands how to do it.....can someone please help me out. THank YOu.

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Bangalore, India
    Your algorithm looks incorrect to me.

    Do the following:
    1.) Read all the data in file1 into memory (Array or Linked List)
    2.) Read all the data in file2 into memory (Array or Linked List)
    3.) Sort the values got from the above 2 steps and store them somewhere.
    4.) Write the stored result to the file3

  5. #5
    Registered User breed's Avatar
    Join Date
    Oct 2001
    The algo looks Ok, but there's not type of sort routine, a slight modification to the suggestion above this post,
    leave you code as is,
    then re-read the fp_out file into memory,
    then sort the file,
    write it back to file,you'll find some help in this post
    Last edited by breed; 06-14-2002 at 03:03 AM.
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    shaik786 and breed have missed the point
    The input files are already sorted - you only need read the next record from each file and decide which one to output when doing a merge.

    As for unregistered
    No idea - it works here - compiled with DJGPP
    Perhaps you need to post how you created your two input files.

    However, having re-read your original post, perhaps shaik786 and breed are onto something.
    You don't actually state that your input files are sorted.
    You have to have sorted your input files for a merge to work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM