hello
i had trouble in finding out whats wrong with my code
I want to sort the data's from my text file and it somehow did but some of the words did not appear.

word.txt
hello
apple
hi
banana
mango

the output I get is :
hello
hello
hi
hi
mango

code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void sort_a(void *array, unsigned n);
static int cmpr(const void *a, const void *b);
int main ( void )
{
   char line[1024];
   const char *line_array[1000];
   int i = 0;
   int j = 0;
   static const char filename[] = "text.txt";
   FILE *file = fopen ( filename, "r" );
   if ( file != NULL )
   {
      while ( fgets ( line, sizeof (line), file ) != NULL )
      {
         if (i < 1024)     
            line_array[i++] = strdup(line);
         else
            break;
            sort_a(line_array, i);
            printf("%s", line_array[j++]);
      }
   }
   else
   {
      perror ( filename ); 
   }
   fclose ( file );
   return 0;
}
int cmpr(const void *a, const void *b) { 
   return (strcmp(*(char **)a, *(char **)b));
}
void sort_a(void *array, unsigned n) { 
   qsort(array, n, sizeof(const char *), cmpr);
}

Thank you!

Recommended Answers

All 2 Replies

You're sorting and printing while building the array, which is not entirely incorrect, but unlikely to display properly. Finish reading the file, then sort and print; you'll get the correct result that way. Here's the corrected code with a few other changes:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static void sort_a(void *array, unsigned n);
static int cmpr(const void *a, const void *b);

int main(void)
{
    static const char filename[] = "input.txt";

    char *line_array[1000];
    char line[1024];
    int i = 0;
    int j = 0;
    FILE *file = fopen(filename, "r");

    if (file != NULL)
    {
        while (fgets(line, sizeof line, file) != NULL)
        {
            // Trim the newline character
            line[strcspn(line, "\n")] = '\0';

            // Stop processing if line_array is full
            if (i < sizeof line_array / sizeof *line_array)
            {
                line_array[i++] = _strdup(line);
            }
            else
            {
                break;
            }
        }

        fclose(file);

        sort_a(line_array, i);

        for (j = 0; j < i; j++)
        {
            printf("%s\n", line_array[j]);
        }

        // Clean up your memory
        for (j = 0; j < i; j++)
        {
            free(line_array[j]);
        }
    }
    else
    {
        perror(filename);
    }

    return 0;
}

int cmpr(const void *a, const void *b)
{
    return (strcmp(*(char **)a, *(char **)b));
}

void sort_a(void *array, unsigned n)
{
    qsort(array, n, sizeof(const char *), cmpr);
}

Ohhhh so that's why i can't sort it out correctly...thanks!

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.