11
Sort a file by line
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAXLINES 10000
- static char *lines[ MAXLINES ];
- static int cmp(const char **s1, const char **s2) {
- return strcmp( *s1, *s2);
- }
- #include <stdlib.h>
- main() {
- char buf[ 4096 ];
- int nlines = 0;
- int i;
- while (fgets(buf, sizeof (buf), stdin) ) {
- char *s = (char *)malloc(strlen(buf) + 1);
- lines[ nlines ] = strcpy(s, buf);
- if ( ++nlines == MAXLINES)
- break;
- }
- qsort(lines, nlines, sizeof(char *),
- (int (*)(const void *, const void *))cmp );
- for (i = 0; i < nlines; ++i)
- fputs(lines[ i ], stdout);
- }
$ gcc -o fslines fslines.c
$ ./fslines < fromages.txt
Abondance
Beaufort
Bleu-d'Auvergne
...
Salers
Selles-sur-Cher
Ste-Maure-de-Touraine
Comments