Sorting numbers in a file
The title is misleading in that the numbers in the file are displayed in a column.
Example:
3
5
10
4
8
1
2
etc...
The resulting file also has to be in that formatting, so:
1
2
3
4
5
6
The problem I am having right now is getting the numbers read from the file and into an array. The problem is that I have it reading character by character so if it is a two digit number, say 10, it will put 1 into one element and 0 into another one. That is one problem I know about. The other major one is how I can print the array, once it is sorted, back into another file with that above formatting. Here is what I have working right now.....
Code:
void quicksort(int *array, int size) //sorting function for the array
{
int a,b,temp;
for(a=0;a<(size-1);a++){
for(b=a=1;b<size;b++){
if(array[a] > array [b]){
temp=array[b];
array[b] = array[a];
array[a] = temp;
}
}
}
}
int main(int argc, char *argv[])
{
FILE *quick, *fp;
char c;
int n;
int array[101];
fp=fopen(argv[1], "r");
quick=fopen("sorted", "w");
while((c=fgetc(fp)) != EOF){ //gets the numbers, put them into an array
for(n=0; n <101; n++){
if(c != '\n'){
array[n] = c;
}
}
}
quicksort(array, 101); //sort the numbers
while(array != NULL){ //Puts the numbers back into a new file
for(n=0; n <101; n++){
fputc(array[n],quick);
}
}
fclose(quick);
system("PAUSE");
return 0;
}