简体   繁体   中英

Sorting array with numbers from text file in C?

So the task is to open a text file(WITHOUT fopen, using only commands in terminal/console), put the info into an array, sort the array(numbers are originally in random order "4,12,2,4") and then then all the sorted data must print on a second txt file.Now I made myself a program but apparently isn't correct, can you tell me where I am wrong, I am using qsort for the first time so I am not expert at this, previously used bubble sort, thanks in advance!!

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

//first time using qsort function.
int array[1024];
int array_counter=0;

int compare (const void * num1, const void * num2)
{
   return (*(int*)num1 - *(int*)num2);
}

int main ()
{
   int i;
   char c;
   while((c=getchar())!=EOF)
   {
      if(c >='0' && c <='9' )
      {
         i = atoi(&c);
         array[array_counter] = i;
         array_counter++;
      }
   }
   int counter;
   qsort(array, array_counter, sizeof(array[array_counter]), compare);
   for(counter = 0; counter < array_counter; counter++)
   {
   printf ("%d", array[array_counter]);
   }
   return 0;
}
   while((c=getchar())!=EOF)
   {
      if(c >='0' && c <='9' )
      {
         i = atoi(&c);
         array[array_counter] = i;
         array_counter++;
      }
   }

This is wrong. You need to put the chars into a buffer, null-terminate it, and then call atoi on the buffer. Or you can use scanf instead of your own loop;

printf ("%d", array[array_counter]);

You want "%d\\n", otherwise the numbers will be smooshed together.

the judge says it's wrong answer

You would know it's the wrong answer if you did some testing before submitting it ... check that the numbers that you're putting into the array have the right values, then check that the output looks right.

I found several mistakes in you code so here is the working code with explanations in as comments (see man page of qsort )

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

//first time using qsort function.
int array[1024];
int array_counter=0;

int compare (const void * num1, const void * num2)
{
   return (*(int*)num1 - *(int*)num2);
}

int main ()
{
   int i;
   char c;
   // use '0' terminated string  for passing it to atoi as this is what it is expecting
   // not only a pointer to char
   char buf[2];
   buf[1] = '\0'; // terminate the string with a '0' since you want only to have digits in your buffer
   // you may use something else (scanf for example) to read number of more than one digit)
   while((c=getchar())!=EOF)
   {
      if(c >='0' && c <='9' )
      {
         // put the char in buf
         buf[0] = c;
         // pass that buf to atoi (even if &c is satisfiying the signature of atoi it is not null terminated)
         i = atoi(buf);
         array[array_counter] = i;
         array_counter++;
      }
   }

   // arguments passed to qsort were wrong (second and third)
   // first is the array (or where the sorting should start)
   // second is the size of one element in the array thus sizof(int)
   // third is the number of the array elements to be sorted
   // the compare function
   qsort(array, sizeof(int), array_counter, compare);

   int counter;
   for(counter = 0; counter < array_counter; counter++)
   {
       // use counter to access array elements not array_counter, array_counter ist the index of the next free element and is always 0
       printf ("%d ", array[counter]);
   }
   return 0;
}

Sample text file digits.txt

2 1 6 4 

execute command

out.exe < digits.txt

output

1 2 4 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM