Hi,
I am facing some problem dealing with the qsort funtion. Basically, the below program takes in a bunch of strings from the text file "word.txt" and then implements the qsort function for sorting the list alphabatically and then displaying the sorted list. However, after the sorting is done, all I'm getting is gibberish stuffs. Can anyone please look at the below code and suggest something or if possible rectify the code..
Regards,
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
FILE *data;
static int compare (const void *, const void *);
int main ()
{
int i;
data=fopen("word.txt","r");
char list[100][20];
char s[20];
int sno1=0;
while((fscanf(data,"%s",&s))==1)
{ strcpy(list[sno1],s);
++sno1;}
int strings_len = sizeof(list) / sizeof(char *);
qsort(list, strings_len, sizeof(char *), compare);
for (i = 0; i < strings_len; i++)
printf ("\n%d: %s.", i, list[i]);
system("pause");
return 0;
}
int compare (const void *a, const void *b)
{
return strcmp( (const char *)a , (const char *)b );
}
And here are the contents of the text file "word.txt"
TEMPERATURE
TEACHER
COMPUTER
ALLOCATE
LOVE
TREE
KEYBOARD
TUTOR
FOLDER
PENCIL
NOTEPAD
DISK
MOUSE
WIRE