I have a text file thats looks like this:
JWF 29
NTK 659
QCU 459
OHK 945
QLI 345
I want a program that will sort it by number highest to lowest by number to look like this:
JWF 29
QLI 345
QCU 459
NTK 659
OHK 945
The things that are constant are that the list will always be 5 items long, the characters will always be 3 characters long, and the input file will always be input.txt
I wrote this program that will read in all the elements in the text file and write them to another file:
Code:
# include <stdio.h>
int main(void)
{
int score[5][5];
int x;
int z;
FILE *outputfile;
FILE *inputfile;
outputfile = fopen("output.txt","w");
inputfile = fopen("input.txt","r");
if(inputfile==NULL)
{
printf("Error: can't find file 'input.txt' ");
system("pause");
return 1;
}
for(x=0;x<=4;x++)
{
for(z=0;z<=2;z++)
{
fscanf(inputfile, " %c", &score[x][z]);
}
fscanf(inputfile, "%d", &score[x][4]);
}
for(x=0;x<=4;x++)
{
for(z=0;z<=2;z++)
{
fprintf(outputfile,"%c",score[x][z]);
}
fprintf(outputfile," %d \n",score[x][4]);
}
return(0);
}
Somewhere in the middle I need to sort it by the numbers in score[0..4][4]
I'm so lost, because all of the columns must retain the same structure, while only moving positions as a whole.