0

I'm writing a C program in Linux that will read in a list of numbers, sort them, and then output them in a text file. My program compiles well enough, but my output is... a bit strange. I've just begun coding in C, so I'm a little overwhelmed with where this error could be coming from.

My input text file that I wrote:

4 3 2 1

My code:

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

void sortNum(int *num, int count){
    int i,j;
    for(i=0;i<=count;i++){
        int minIndex=i;
        for(j=i+1;j<=count;j++){
            if(num[j] < num[minIndex]){
                minIndex=j;
            }
        }
        int temp = num[minIndex];
        num[minIndex]=num[i];
        num[i]=temp;
    }
}

int main(int argc, char *argv[]){
    int *numbers;
    int count=0;
    int i=0;
    char input[20], output[20];
    FILE *fileIn, *fileOut;

    numbers = (int *)malloc(sizeof(int));
    if(argc != 3){
        printf("Please provide the input and output text file names as %s name1 name2\n", argv[0]);
        return 0;
    }
    else{
        sscanf(argv[1], "%s", input);
        sscanf(argv[2], "%s", output);
    }

    fileIn = fopen(input, "r");
    fileOut = fopen(output, "w");

    if(!fileIn){
        printf("Input file %s cannot be opened.\n", input);
        return 0;
    }
    else if(!fileOut){
        printf("Output file %s cannot be written.\n", output);
        return 0;
    }
    else{
        while(!feof(fileIn)){
            i++;
            numbers = (int *)realloc(numbers, 4 * sizeof(int));
            fscanf(fileIn, "%d", &numbers[i]);
            printf("%d ", numbers[i]);
        }
    }
    count = i;
    free(numbers);
    fclose(fileIn);
    printf("\n");
    sortNum(numbers, count);
    printf("\nElements are now sorted: \n");

    for(i=0; i <= count; i++){
        fprintf(fileOut, "%d", numbers[i]);
        printf("%d ", numbers[i]);
    }
    printf("\n");
    fclose(fileOut);
    return 0;
}

And here is my output that I get when I run the program:

4 3 2 1 134409 Elements are now sorted: 0 1 2 3 4 134409

For starters, I'm not sure where the 0 came from when writing, nor where or what 134409 is supposed to represent.

If done correctly, the desired output should be something like:

4 3 2 1 Elements are now sorted: 1 2 3 4

As far as troubleshooting goes, I really wish I had more to offer, but I'm truly lost. Perhaps I've overlooked something with the library functions? I originally thought that it may have something to do with how I wrote my while loop, but now I'm not so sure. I'd really appreciate some guidance.

  • 4
    At first glance `for(i=0; i <= count; i++)` should probably `for(i=0; i < count; i++) ` and `for(j=i+1;j<=count;j++)` likely `for(j=i;j – 138 Sep 26 '18 at 21:52
  • 1
    From a quick glance, the `<=` in your for-loops should be `<`, to fix an off-by-one bug. – nanofarad Sep 26 '18 at 21:52
  • Your memory handling is all over the place. Freeing memory and then using it (free before sortNum) Allocating 4 ints for each integer (and why realloc every time? That is just bad) – John3136 Sep 26 '18 at 21:55
  • Also, our old friend: https://stackoverflow.com/q/5431941/758133 – Martin James Sep 26 '18 at 22:04

0 Answers0