Ryad Kovach
Ryad Kovach

Reputation: 29

C : Input file does not sort in array

could I get any hint about my code ? All i get is empty black screen.

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

#define MAX 20
int main()
{
int num,c, i, j, temp, array[20];

FILE *fp;
fp = fopen("input.txt", "r");  //Opening a file
if (fp == NULL)
{
    printf("File empty! \n");
    return 1;
}

while(i < MAX)
{
    fgets(fp, "%d", &array[i]);
    printf("\nARRAY %d",array[i]);
    i++;
}
fclose(fp);
*
*
*more code will be added

return 0;
}

Input.txt contains: 0005 0006 FFFF 0007 0003 FFFF 0004 0002 0001 FFFF 0000

Also, when the input.txt is empty, does not give error. I am trying to add these numbers from input into array[i] so how could sort them. Any hint/help will help. Thank you.

Upvotes: 0

Views: 72

Answers (1)

WhozCraig
WhozCraig

Reputation: 66194

Among the things wrong in your code:

  • i is uninitialized. Evaluating uninitialized data invokes undefined behavior, the root of most evil in C programs (and pretty much anywhere else). Severity: critical.
  • The call to fgets is not correct. The parameter list you're providing suggests you should be using fscanf. Severity: Fatal (will not compile).
  • The format flag for reading hexadecimal data is wrong. It should be %X. Severity: Medium-High, your code will stop reading as soon as the first FFFF is encountered, thus giving you inaccurate results.
  • The reading operation should be checked for success. Severity: Medium-High. You should stop reading once no more values can be scanned in.

There are other things that, though not critical, are advised. Your sizing counter is int, but is supposed to represent a magnitude of objects. Thus it should be an unsigned type. The standard library provides a type it uses for such operations, size_t, and I advise you use it here.

All of the above rectified in the following, which includes a braindead-simple bubble-sort to perform your actual sorting operation (the algorithm of which is available in roughly a million places on the web).

Code

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

#define MAX 20

void bubblesort(int ar[], size_t count);

int main()
{
    int array[MAX];
    size_t count=0;

    // open input file
    FILE *fp = fopen("input.txt", "r");  //Opening a file
    if (fp == NULL)
    {
        printf("File empty! \n");
        return EXIT_FAILURE;
    }

    // read all integers from the file
    for (count=0; count<MAX && fscanf(fp, "%X", array+count) == 1; ++count)
        printf("%04X ", array[count]);
    putc('\n', stdout);

    // close file, no longer needed
    fclose(fp);

    // sort the integers
    bubblesort(array, count);

    for (size_t j=0; j<count; ++j)
        printf("%04X ", array[j]);
    putc('\n', stdout);

    return EXIT_SUCCESS;
}

// simple bubblesort
void bubblesort(int ar[], size_t count)
{
    int swapped = 1;
    while (count-- && swapped)
    {
        swapped = 0; // reset swap flag
        for (int i=0; i<count; ++i)
        {
            if (ar[i] > ar[i+1])
            {
                int tmp = ar[i];
                ar[i] = ar[i+1];
                ar[i+1] = tmp;
                swapped = 1; // swapped; set flag
            }
        }
    }
}

Input (from input.txt)

0005 0006 FFFF 0007 0003 FFFF 0004 0002 0001 FFFF 0000 

Output

0005 0006 FFFF 0007 0003 FFFF 0004 0002 0001 FFFF 0000 
0000 0001 0002 0003 0004 0005 0006 0007 FFFF FFFF FFFF 

Best of luck.

Upvotes: 1

Related Questions