This is the code I have so far, took me along time to learn from googling. so basically, I open the file, read it, pass to a array, then sort it.
It's long, so thanks for reading it.
Code:
#include <stdlib.h>
#include <stdio.h>
int main()
{
/*Promot user to enter file name*/
char infile_name[20];
printf("File:");
scanf("%s", infile_name);
printf("\n");
/* Open the file */
FILE *infile = fopen(infile_name, "r");
if (!infile)
{
/* If the operation was unsuccessful */
printf("unable to open %s\n", infile_name);
exit(1);
}
/* Read the file */
int in;
char instr;
do
{
fscanf(infile, "%d", &in); /* read from infile into int variable in */
if (!feof(infile)) /* only process in if didn't reach end of file */
{
instr=in; /* pass In to a array*/
}
} while(!feof(infile));
fclose(infile);
/* Sorting */
int compare (const void *s, const void *t)
{
return (*(const int*)s - *(const int*)t);
}
int count =sizeof(instr)/sizeof(char);
qsort(instr, count, sizeof(int), (*compare)(const void *, const void *));