Z
zavnobih
Below is my code. Qsort is not working. What is wrong?
************************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#define MAX 10
int compare( const void *arg1, const void *arg2 );
typedef struct
{
char s_line[20];
} lines_to_sort_t;
int main (int argc, char **argv)
{
int c;
FILE *fp;
char text_ln[20];
lines_to_sort_t table[MAX];
while ( (c = getopt(argc, argv, "r") ) != EOF )
{
switch (c)
{
case 'r':
printf("aaa \n");
break;
}
}
int i=0;
while (optind < argc)
{
fp = fopen(argv[optind], "r");
while ( fgets(text_ln, sizeof(text_ln), fp) != NULL)
{
strcpy(table.s_line, text_ln);
i++;
}
optind++;
}
qsort(table, sizeof table / sizeof table[0], sizeof table[0],
compare);
i = 0;
while (i < 3 )
{
printf( "%d: %s",i, table.s_line );
i++;
}
}
int compare( const void *arg1, const void *arg2 )
{
const lines_to_sort_t *p1 = arg1;
const lines_to_sort_t *p2 = arg2;
return strcmp(p1->s_line, p2->s_line);
}
************************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#define MAX 10
int compare( const void *arg1, const void *arg2 );
typedef struct
{
char s_line[20];
} lines_to_sort_t;
int main (int argc, char **argv)
{
int c;
FILE *fp;
char text_ln[20];
lines_to_sort_t table[MAX];
while ( (c = getopt(argc, argv, "r") ) != EOF )
{
switch (c)
{
case 'r':
printf("aaa \n");
break;
}
}
int i=0;
while (optind < argc)
{
fp = fopen(argv[optind], "r");
while ( fgets(text_ln, sizeof(text_ln), fp) != NULL)
{
strcpy(table.s_line, text_ln);
i++;
}
optind++;
}
qsort(table, sizeof table / sizeof table[0], sizeof table[0],
compare);
i = 0;
while (i < 3 )
{
printf( "%d: %s",i, table.s_line );
i++;
}
}
int compare( const void *arg1, const void *arg2 )
{
const lines_to_sort_t *p1 = arg1;
const lines_to_sort_t *p2 = arg2;
return strcmp(p1->s_line, p2->s_line);
}