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;
}