I have to sort this :
char ** lu
It contains line like this:
807 qmuftnqrjalreihiqblaplydrixyvzenlasfojyxlloptyakbnmqymwputqstufguylkjlkux
Each line have a number at the beginning, i need to sort them with it
But i dont know how to use strtol is the cmp_fct of quicksort
Code sample:
char ** lu = NULL;
int nlu = 0;
size_t n_byte = 10000*8; // 10000 octects par ligne cf:énoncé
void lire(FILE * from){ //rm data.out && ./a.out < data.in > data.out && cmp -b data.in data.out
char* ligne = malloc(n_byte);
lu = malloc(n_byte);
assert(lu != NULL);
assert(ligne != NULL);
while(getline(&ligne, &n_byte, from) != -1) // getline return -1 on failure to read a line (including end-of-file condition)
{
if (ligne[strlen(ligne) - 1] != '\n')
fprintf(stderr, "Aie ! Ligne trop longue (%d max)\n", MaxCar);
else
ligne[strlen(ligne) - 1] = '\0';
if (nlu == MaxLig)
{
fprintf(stderr, "Aie ! Trop de lignes (%d max)\n", MaxLig);
break;
}
lu[nlu] = malloc(n_byte);
assert(lu != NULL);
assert(lu[nlu] != NULL);
memcpy(lu[nlu], ligne, n_byte); /* /?\ + standard que strdup /?\*/
if (lu[nlu] == NULL){
fprintf(stderr, "Aie ! Plus de mémoire\n");
exit(1);
}
nlu += 1;
}
}
static int int_cmp(const void *a, const void *b)
{
const long ia = strtol(a, NULL, 10); // casting pointer types
const long ib = strtol(b, NULL, 10); // casting pointer types
printf("\n ia =%ld", ia);
printf("\n ib =%ld", ib);
if(ia < ib)
return -1;
return ia > ib;
}
void
ecrire(FILE * to){
int i;
for(i = 0; i < nlu; i++)
printf("%s\n", lu[i]);
}
int
main(int ac, char * av[]){
lire(stdin);
qsort(lu, nlu, sizeof(lu[0]), int_cmp);
ecrire(stdout);
return 0;
}
How I use Quicksort :
qsort(lu, nlu, sizeof(lu[0]), int_cmp);
nlu is the number of line to be sorted
I think i have to use a and b in strtol(). but strtol show me wrong index (0 or 1)
Prototype of strtol(): long int strtol(const char *str, char **endptr, int base)
PROBLEME HERE:(for the test of int_comp)
ia =0 ib =0 ia =0 ib =0 ia =0 ib =0 ia =0
Thanks for help