qsort+structures: Cant figure what's wrong

This is a discussion on qsort+structures: Cant figure what's wrong within the C Programming forums, part of the General Programming Boards category; Hi there, I'm trying to sort an structure with qsort. I'm reading the structure from a binary file. It's a ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    12

    qsort+structures: Cant figure what's wrong

    Hi there, I'm trying to sort an structure with qsort. I'm reading the structure from a binary file.

    It's a structure of ~ 60000 members, there are a couple of calculations in between which assign rmsd values for each member (this works fine),
    and finally (ideally) the structure is sorted by lib.rmsd.
    I couldnt have this last step working, as only the last member of the structure is listed, and with no rmsd value. I just dont know where to look anymore.
    There are a couple of functions I havent listed in order to keep it as simple as possible.
    Any help would be really appreciated,
    thanks in advance



    Code:
    typedef struct library59k { 
       int numModel;          
       char combination[6];
       float rmsd;
       float coordinates[28][3];
    } lib; 
    
    int struct_cmp_by_rmsd(const void *a, const void *b)
    {
        lib *ia = (lib *)a;
        lib *ib = (lib *)b;
        return (int)(ia->rmsd - ib->rmsd);
    } 
    
    void print_struct_array(lib *array, size_t len)
    {
        size_t i;
        for(i=0; i<len; i++)
            printf("%5i %s %.5f\n", array[i].numModel, array[i].combination, array[i].rmsd);
    }
    
    int main()
    {
    	 
    lib member;
    lib *p;
    p = &member;		
    
    float coord_target[28][3], modlib_extreme[8][3], target_ext[8][3];//alineado[28][3], 
    int j,k,l=1,totalresidues;
    FILE *ptr59k, *out; 
    
    
    
    ptr59k = fopen( "59kbin.dat", "rb" );
       if ( ptr59k == NULL ) {
          printf( "cant open file.\n" );
       } 
     
    out = fopen( "outfile", "w" );
       if ( out == NULL ) {
          printf( "cant open file.\n" );
       } 
          
    totalresidues=GetCoordPDBdata(coord_target,l);
    fprintf(out,"cantres %i\n",totalresidues);
    
    for(k=START;k<2;k++)
    {
    	fprintf(out,"window %i\n",l++);
    	
          while ( !feof( ptr59k ) ) 
    	  { 
             fread(p, sizeof( lib ), 1, ptr59k );
    
             if ( p->numModel != 0 )
    		 {
    			coord_extreme(p->coordinates,modlib_extreme);
    			
    			GetCoordPDBdata(coord_target,l);
    			
    			coord_extreme(coord_target,target_ext);
    			
    			p->rmsd=superposicion(target_ext,modlib_extreme);
    			
                           
    			fwrite( p, sizeof( lib ), 1, ptr59k );
    			
                fprintf(out, "%5i ", p->numModel );
    			for(j=0;j<5;j++) fprintf(out,"%c",p->combination[j]);
    			fprintf(out, " %.6f\n", p->rmsd );
    			
    		} 
          } 
    
    rewind(ptr59k);
    	size_t member_len = sizeof(member) / sizeof(lib);
     
          qsort(p, member_len, sizeof(lib), struct_cmp_by_rmsd);
     
    	print_struct_array(p, member_len);
    			
    }
    
    fclose(out);
    fclose(ptr59k);
     
       return 0;
     }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    int struct_cmp_by_rmsd(const void *a, const void *b)
    {
        lib *ia = (lib *)a;
        lib *ib = (lib *)b;
        return (int)(ia->rmsd - ib->rmsd);
    }
    I'm not sure if this is a good way to compare floats. What if their difference is less than 1 but more than 0?

    As for only one item being sorted - I don't see you allocating an array anywhere. All you have is a single lib instance member, and a pointer that refers to this single instance. Also because of that the following should not be nothing but 1

    Code:
    size_t member_len = sizeof(member) / sizeof(lib);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    Code:
    qsort(p, member_len, sizeof(lib), struct_cmp_by_rmsd);
    p points to one struct (specifically, to member). It doesn't make sense to sort one single object.

    There's also this:
    Code:
    size_t member_len = sizeof(member) / sizeof(lib);
    member_len is being passed to qsort() as the parameter that specifies the number of elements to be processed. member is a lib, so sizeof(member) / sizeof(lib) is always 1. This is, of course, the correct number of elements in your "array", but the calculation doesn't make sense. If member were an array, the calculation might make sense (if all elements of the array were being used), but currently, it doesn't. You're not actually creating anything to sort with this code.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    12
    I realized the errors a couple of hours after I posted.
    thanks for the answers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and dynamically allocated arrays
    By Bandizzle in forum C Programming
    Replies: 7
    Last Post: 10-04-2009, 03:05 PM
  2. What am I doing wrong ?
    By scottjge in forum C Programming
    Replies: 6
    Last Post: 10-01-2009, 12:55 PM
  3. Replies: 0
    Last Post: 11-11-2001, 01:24 PM
  4. cant figure out whats wrong with this ....
    By ii3ejoe in forum C++ Programming
    Replies: 17
    Last Post: 10-22-2001, 01:47 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21