The sort function in the C programming language allows the SORT/MERGE facility in the MCP to be called from C programs. This facility provides the capability to sort records from a file or a function. Output can be sent to a file or a function. The records can be sorted in ascending or descending order, and they can be sorted using a user-defined collating sequence. Ordering can be done by specifying keys in a record or by providing a function that performs comparisons between records. The sort can be performed by using memory only, memory and disk, memory and tape, or all three. Sort supports restart capabilities for disk sorts.
The sort parameters are described in the following table:
Parameter |
Description |
---|---|
sort_struct |
This parameter points to a structure that contains information that describes the operation of the sort. It contains members that describe the record size, types of input and output (function or file), the keys, workfiles (if any), restart options, internationalization options, and an optional translate table that specifies an alternate collating sequence. The constant structure_sort_default_info contains the default values for the members of_c_info. |
Input |
This parameter is a pointer to either a file or a function. If it is a file pointer, the file referenced by the pointer is read to provide the input to the sort. If it is a pointer to a function, the function is called for each record to be provided to the sort. The function takes no arguments and must return a pointer to the input data record. An example of an input function follows the program example after the description of the comparison function. |
Output |
This parameter is a pointer to either a file or a function. If it points to a file, the output from the sort is written to that file. If the output parameter points to a function, that function is called once for each record to be output. The output function needs two arguments: the first is a const pointer to the output record, the second is a const int length. An example of an output function follows the program example after the description of the comparison function. |
Comparison |
This parameter is a pointer to the user-specified compare function. It is optional and is used only if requested in the info structure. If used, it is called every time two records are to be compared. This provision allows for more complex comparisons. The comparison function must take three parameters: a pointer to each of the two records being compared, and an integer length. The function must return an integer. The value returned by this function is consistent with the value returned by the compare function supplied to the qsort function(,stdlib.h>): the function must return an integer less than, equal to, or greater than zero if the first record is considered to be respectively less than, equal to, or greater than the second. |
C SORT Example
The following is an example of a C program that calls the SORT utility. In the example, multiple keys are used (as opposed to a comparison function),input to and output from the sort is by way of file pointers. Note that in order to use these examples, the input file, whose name is defined by the macro inputfile, must exist and have records of length rec_len_def characters. An output file will be created. Its name is found in the definition of the outputfile macro. Following this example are smaller examples of options not covered in this example, such as comparison, input, and output functions.
#include <errno.h> #include <stdio.h> #include <sort.h> #include <string.h> #include <stdlib.h> #define rec_len_def 72 #define inputfile "sort/in" /* name of the input file */ #define outputfile "sort/out" /* name of the output file */
/* driver */ main() { FILE * infileptr; FILE * outfileptr; char buffer[100][ char *bufptr = buffer[0]; int int_ary [10]; int i, len; int *int_ptr; char * charptr; struct _c_info info_structure_1; /* sorting with 2 keys - key 1, col 0- 9, ascending key 2, col 10-19 descending. input from a file output to a file */ if ((infileptr = fopen (inputfile, "r")) ** NULL) { printf ("can't open input file.\n"); exit (EXIT_FAILURE); } info_structure_1 = _sort_default_info; info_structure_1.sort_input = _sort_file; info_structure_1.output = _sort_file; info_structure_1.rec_len = _rec_len_def; info_structure_1.core_size = 0;
/* set up keys info_structure_1.compare info_structure_1.num_keys info_structure_1.sort_keys[0].start_pos = 0; info_structure_1.sort_keys[0].length = 10; info_structure_1.sort_keys[0].seq = _sort_ascending; info_structure_1.sort_keys[0].type = _sort_type_alpha; info_structure_1.sort_keys[1].start.pos = 10; info_structure_1.sort_keys[1].length = 10; info_structure_1.sort_keys[1].seq = _sort_descending; info_structure_1.sort_keys[1].type = _sort_type_alpha;
errno = 0; sort (&info_structure_1, infileptr, outfileptr); if (errno) perror ("sort error "); else printf ("sort completed o.k. \n"); /* end of sort testing */ /* end of sort testing
Example of Compare Function
/* compare function */ int compare_func (char * rec_1, char * rec_2, int len) { for ((; *rec_1 ** *rec_2; rec_1++, rec_2++) if (*rec_1 ** '\0') return 0; return *rec_1 - *rec_2; }
Example of Input Function
/* input function */ char * inproc () { static char buffer[100]; printf ("enter the input record \n"); return (gets (buffer)); }
Example of Output Function
/* output function */ int output_func (const char * outptr, const int len) { printf ("Output Rec: %s\n", outptr); }
For more detailed information, refer to the C Programming Language Reference Manual.