Ok, so I have a file with integers, like:
14
22
82
53
61
74
47
95
and I want to copy it into my structure, the problem being that my structure has 2 columns, and I don't know how to copy the file into just one.
My question is: is there a quick and easy function like qsort that will automatically copy my file over?
#include <stdio.h> /* printf */
#include <stdlib.h> /* qsort */
struct Element
{
int userId;
int score;
};
struct Element elements[] = {
{1, 13},
{2, 9},
{3, 13},
{4, 19},
{5, 8},
{6, 11},
{7, 14},
{8, 17},
};
int ascendingSortCompareFunction (const void * a, const void * b)
{
return (((struct Element *)a)->score - ((struct Element *)b)->score);
}
int descendingSortCompareFunction (const void * a, const void * b)
{
return ((struct Element *)b)->score) - (((struct Element *)a)->score;
}
int main ()
{
int n;
int count;
count = sizeof(elements) / sizeof(elements[0]);
qsort(elements, count, sizeof(elements[0]), ascendingSortCompareFunction);
printf ("UserID\tScore (Ascending Sort)\n");
for (n = 0 ; n < count ; n++)
printf ("%d\t%d\n", elements[n].userId, elements[n].score);
qsort(elements, count, sizeof(elements[0]), descendingSortCompareFunction);
printf ("UserID\tScore (Descending Sort)\n");
for (n = 0 ; n < count ; n++)
printf ("%d\t%d\n", elements[n].userId, elements[n].score);
getchar();
return 0;
}