Thread: I need to sort a File..

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    61

    I need to sort a File..

    Hi everyone.. Can you help me with this problem. I want to sort a file but after sorting all my data are lost and I get many 000000000. Please help.

    Here's my code...

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<ctype.h>
    
    
    struct info
    {
      char pnum[50];
      char pdes[50];
      float price;
    } e, sort[100];;
    
    
    void view()
    {
     FILE *fp = fopen("master.dat","r");
     while((fscanf(fp, "%s\t\t%[^\t]\t\t\t%f\n", &e.pnum, &e.pdes, &e.price)) != EOF)
     {
    	printf("%s\t\t%s\t\t%0.2f\n", e.pnum, e.pdes, e.price);
     }
     fclose(fp);
    }
    
    
    void sorting()
    {
      int count1, count2, count3 = 0;
      char save1[50];
      char save2[50];
      float save3 = 0;
      FILE *fp = fopen("master.dat", "r");
      FILE *temp = fopen("temp.dat", "w");
    
    
      while((fscanf(fp, "%s\t\t%[^\t]\t\t\t%f\n", &e.pnum, &e.pdes, &e.price)) != EOF)
      {
    	strcpy(sort[count3].pnum, e.pnum);
    	strcpy(sort[count3].pdes, e.pdes);
    	sort[count3].price = e.price;
    	count3++;
      }
    
    
      for(count1 = 0; count1 < count3; count1++)
      {
    	for(count2 = count1 + 1; count2 < count3; count2++)
    	{
    		if(strcmp(sort[count1].pnum, sort[count2].pnum) > 0)
    		{
    			strcpy(save1, sort[count1].pnum);
    			strcpy(save2, sort[count1].pdes);
    			save3 = sort[count1].price;
    
    
    			strcpy(sort[count1].pnum, sort[count2].pnum);
    			strcpy(sort[count1].pdes, sort[count2].pdes);
    			sort[count1].price = sort[count2].price;
    
    
    			strcpy(sort[count2].pnum, save1);
    			strcpy(sort[count2].pdes, save2);
    			sort[count2].price = save3;
    		}
    	}
      }
    
    
      for(count1 = 0; count1 <= count3; count1++)
      {
    	fprintf(temp, "%s\t\t%s\t\t%0.2f\n", sort[count1].pnum, sort[count1].pdes, sort[count1].price);
      }  
       
      remove("master.dat");
      rename("temp.dat","temp.dat");
      fclose(fp);
      fclose(temp);
    }
    
    
    int main()
    {
     sorting();
     view();
     getch();
     return 0;
    }
    MASTER.DAT
    Code:
    IBMGE12000	IBM 101 Motherboard	4500.00
    BOKA118001	BOKA 118 Speakers	3500.00
    ASUS101112	ASUS 110 Motherboard	4500.00
    BOKA118001	BOKA 118 Speakers	3500.00

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    while((fscanf(fp, "%s\t\t%[^\t]\t\t\t%f\n", &e.pnum, &e.pdes, &e.price)) != EOF)
    In your other thread you've said that your program works but you still have the same error. Haven't you read whiteflags' post?

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    I think that line of code is working fine..

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    My problem in my last thread is every time I fscanf the value of my e.pdes becomes "ASUS 101 Motherboard 4500.00" description and the price. And the value of my e.price become 0.00

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Carefully read whiteflags' post again (especially the beginning) or compile with all warnings turned on and then think about it again.

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The current line #18 should be pushed down, making room for code to test if the file was opened or not:
    Code:
    if(!fp) {
       printf("Error! File did not open\n");
       return 1;
    }
    Then looking at your data you got from the file - is it printing out OK, or not?

    Your sorting logic is Substitution sort, but it should be tweaked just a bit, and made easier. Of course, this is C, and the standard simple iterators for loop is i and j, so let's use them, and get used to them - your mind will adjust trust me.

    Code:
    int i,j;
    int max = count3; //"count3" is an ambiguous name
    struct info temp;
    
    for(i=0;i<max-1;i++) {
       for(j=i+1;j<max;j++) {
          if((strcmp(sort[i].pnum,sort[j].pnum)) > 0)  { //compare
              temp=sort[i];                              //swap entire struct
              sort[i]=sort[j];
              sort[j]=temp;
          }
       }
    }
    The above is off the cuff, so may have minor errors, but note how the WHOLE struct can be moved at once in the swap.

    Try working along those lines - but start with checking that you are receiving the data OK, from the file.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    Thanks Adak! : )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sort file
    By quo in forum C Programming
    Replies: 13
    Last Post: 02-17-2013, 03:27 PM
  2. Sort my file
    By Gil Carvalho in forum C Programming
    Replies: 13
    Last Post: 06-21-2012, 01:32 PM
  3. File I/O with sort
    By yigster in forum C Programming
    Replies: 2
    Last Post: 06-08-2010, 08:29 PM
  4. Sort a txt File
    By Achilles in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 06:39 AM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM