ok im trying to sort a bunch of numbers from a file. the file is stated via argv[2] and i want to sort the first n numbers of the file. n is equal to argv[1].
Whenever so the imput i have been using is ./a.out 5 testdata
when th code is run its printing out just a new line. I know there nothing wrong with the printf statements and im pretty sure it has something to do with declaring my arrays.
Here is my code
I've basicly narrowed it down to either my strtol fuction or malloc. Im leaning toward malloc on accounts that im not 100% sure i know what is is im doing with that.
Any help would be agreatly apreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
int *value ;
int num = strtol(argv[2], NULL, 10);
int temp ;
FILE *fin = NULL ;
int i;
int k;
value = malloc( sizeof (num) * num);
fin = fopen(argv[2],"r");
for( i = 0; i <=num; i++){
fscanf(fin, "%d", &value[i]);
}
for(k = 0; k < num; k++){
if(value[k + 1] > value[k]){
temp = value[k];
value[k] = value[k+1];
value[k+1] = temp;
}
else
value[k] = value [k];
}
for(j = 0; j < num; j++){
printf("%d", value[j]);
}
printf("\n");
return 0;
}