BL> I want to read a line out of FILES, put it in a pointer array, sort
BL> the line using the first 4 characters, and then write the sorted array
BL> to the new file.
Time for another use of char **. :-) There is an example program,
xysort.c in OZPD, which does exactly what you are doing.
BL> This is the qsort() function... strcmp() straight out of the book.
BL> int sort_function(const void *a, const void *b)
BL> {
BL> return(strcmp((char *)a,(char *)b));
BL> }
If you had a char list[500][50]; and were trying to sort it, then the above
method would work. You're not, you're trying to sort char *list[500]. To
do that, you need:
int sort_function(const void *a, const void *b)
{
return(strcmp(*(char **)a, *(char **)b));
}
Now, YOU tell ME why you used char ** instead of char * !!! It's perfectly
fine to do, it's just you've answered your own question.
BL> My problem is with qsort(). It gets totally stuffed up with the long
BL> and variable-length lines. How the bloody hell can I make it sort
BL> on just the first few characters in each line? When I try to rewrite
I think you asked the wrong question. The answer to that question is use
strncmp() and specify 2, 3, 4 or whatever as the length to look at.
BL> sort_function() it won't let me truncate the strings because it's
BL> passing void pointers!
You can truncate strings simply by going *(p + 2) = '\0'. I don't think
you want to truncate strings though. BFN. Paul.
@EOT:
---
* Origin: X (3:711/934.9)
|