[REBOL] Re: Sort by first part of line
From: reffy:ulrich at: 6-Sep-2002 18:53
-- Attached file included as plaintext by Listar --
That sounds like a fast machine you have Andrew.
I would like for you to run a test if you would.
SOURCE CODE of what I am sending:
/* QSORT.C: This program reads the name of a file
* to be sorted, from the command line. The lines
* are read into memory then sorted and then written
* to stdout.
*
* Note: The sort is of the entire ASCII line and does
* not parse the line in any way.
*
* Sample:
* qsort sortme.txt > sorted.txt
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#define MAX_LINE_SIZE 1024
int compare( const void *arg1, const void *arg2 );
char line[MAX_LINE_SIZE];
char ** lines;
int lcnt = 0;
FILE * stream = NULL;
time_t start, finish;
double elapsed_time;
void CountTheLines(char *);
void ReadTheLines(char *);
void SortTheLines(void);
void OutputTheLines(void);
void main( int argc, char **argv )
{
if (argc != 2)
{
printf("Usage: %s nameoffiletosort\n", argv[0]);
exit(1);
}
time( &start );
CountTheLines(argv[1]);
ReadTheLines(argv[1]);
SortTheLines();
OutputTheLines();
time( &finish );
elapsed_time = difftime( finish, start );
printf( "\nSorted %d lines in %6.0f seconds.\n", lcnt, elapsed_time );
}
void CountTheLines(char *fn)
{
// Count the lines in the file
stream = fopen(fn, "r");
if(stream == NULL)
{
printf( "Unable to open input file\n" );
exit(1);
}
while (fgets(line, MAX_LINE_SIZE, stream) != NULL)
lcnt++;
fclose(stream);
// Allocate a place to put the lines
lines = (char **)malloc(lcnt*sizeof(char *));
}
void ReadTheLines(char *fn)
{
// Read lines into memory
lcnt = 0;
stream = fopen(fn, "r");
if(stream == NULL)
{
printf( "Unable to open input file\n" );
exit(1);
}
while (fgets(line, MAX_LINE_SIZE, stream) != NULL)
lines[lcnt++] = strdup(line);
fclose(stream);
}
void SortTheLines()
{
// Sort the lines
qsort( (void *)lines, (size_t)lcnt, sizeof( char * ), compare );
}
void OutputTheLines()
{
int i;
// Output the sorted lines
for( i = 0; i < lcnt; ++i )
printf( "%s", lines[i] );
}
int compare( const void *arg1, const void *arg2 )
{
/* Compare all of both strings: */
return _stricmp( * ( char** ) arg1, * ( char** ) arg2 );
}
Download NeoPlanet at http://www.neoplanet.com
-- Binary/unsupported file stripped by Listar --
-- Type: Application/Octet-Stream