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

In this case, you already know the number of elements (min(100,sno1)) and size of each element (20). Use those values instead of the computed values, besides the fact that the size argument is not the size of a char*, but is 20. So, your qsort call should be this:

qsort(&list[0][0], sno1, 20, compare);

Also, your while function should read like this - skipping the copy part:

while((fscanf(data,"%s",&list[sno1][0]))==1)
{
    ++sno1;
}
commented: Thanks! This solved my problem! +0

@rubberman : Thanks mate! Your suggestions worked! Problem solved! Greetz

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.