0

I have a queue with int numbers, the goal is to print all elements sorted. First, I save all numbers in a txt file, and then I use the shell command "sort" to print all of them sorted. It's possible to do this in a cleaner way? (without using files, and if it's possible without system(...);)

This is the code:

   ...
   FILE* fp=fopen("numbers.txt","w+");
   printf("\n");
   while (!empty(&my_queue)) //while queue is not empty
   {
      elem = first(&my_queue); //first() gets and deletes first element of queue
      fprintf(fp,"%d\n", elem->number);
   }
   fclose(fp);
   system("sort -n < numbers.txt");
   remove("numbers.txt");
}

Thanks!

  • 2
    What about googling before asking ? http://stackoverflow.com/questions/1787996/c-library-function-to-do-sort – hivert Feb 15 '14 at 15:13
  • I think I can't use that function for this, because the nodos of the queue are not consecutively in the memory –  Feb 15 '14 at 15:44
  • Can you take the queue values, copy them into a continuos memory, sort, and make a queue of that? – Guilherme Bernal Feb 15 '14 at 16:01
  • But I have to use a malloc to put the numbers into a continuous memory, and I don't know how much elements I have –  Feb 15 '14 at 16:16

1 Answers1

0

Supposing your numbers are floats listed separate lines of your file:

  1. Create an array of float. If your don't know how many numbers you have, use a very large array (Note: in the example below, I created an array number_array of size 100)
  2. Read your file
  3. For each line of your file, store the number in the array you created in step 1
  4. After reading the whole file, sort the array. Note that in the example below, I use the function qsort to sort the array (see manual with command man qsort ;) )
  5. Write the content of your sorted array in your file

Example:

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

#define LINE_SIZE 1024

// Comparison function used for sorting
int compare (const void * a, const void * b)
{
    float fa = *(float*) a;
    float fb = *(float*) b;
    return (fa > fb) - (fa < fb);
}

// -------------------------------------------------------------
int main()
{
    FILE *f;
    char line[LINE_SIZE], *p;

    int i = 0, j;

    // YOUR NUMBER ARRAY
    float number_array[100];

    // Open file
    if (!(f = fopen("numbers.txt", "r"))) {
        fprintf(stderr, "Unable to open file argument\n");
        return 1;
    }

    // Read lines from file
    while (fgets(line, LINE_SIZE, f)) {
        // You may want to remove the trailing '\n'
        if ((p = strchr(line, '\n'))) { *p = '\0'; }

        // Skip empty lines
        if (line[0] == '\0') { continue; }

        // Adding number to array
        number_array[i] = atof(line);  
        i++;
    }

    // Closing file
    fclose(f);

    // Sorting array
    qsort (number_array, i, sizeof(float), compare);

    // Displaying result 8-)
    // for (j=0; j<i; j++)
    // {
    //     printf("%0.2f ", number_array[j]);
    // }
    // printf("\n");

    // Writing sorted result to file
    if (!(f = fopen("numbers.txt", "w"))) {
        fprintf(stderr, "Unable to open file argument\n");
        return 1;
    }

    for (j=0; j<i; j++)
    {
        fprintf(f, "%0.2f\n", number_array[j]);
    }

    fclose(f);


    return 0;
}
Littm
  • 4,923
  • 4
  • 30
  • 38