Hope this helps you....
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 128
typedef struct _Person
{
int grade;
char *name;
}Person;
//Functions.
Person *GetRecordsFromFile(char *filename, int *Counted)
{
if(!filename)
{
printf("Wrong Filename, enter a valid one.\n");
return NULL;
}
else
{
FILE *f = fopen(filename, "r");
if(!f)
{
printf("Can not open file %s\n", filename);
return NULL;
}
else
{
char *buffer = (char *)calloc(MAX_SIZE, sizeof(char));
if(!buffer)
{
printf("Memory Error.\n");
return NULL;
}
else
{
int i = 0;
int j = 0;
int index = 0;
char *pointer = NULL;
char *p = NULL;
Person *per = (Person *)calloc(1, sizeof(Person));
if(per == NULL)
return NULL;
else
{
while(fgets(buffer, MAX_SIZE, f) != NULL)
{
if(buffer[strlen(buffer) - 1] == '\n')
buffer[strlen(buffer) - 1] = '\0';
//Have the buffer, parse the data.
pointer = strchr(buffer, ' ');
if(pointer == NULL)
{
printf("Wrong format in %s\n", filename);
break;
}
else
{
index = pointer - buffer;
index = index < 0 ? -index : index;
per[i].name = (char *)calloc(index + 1, sizeof(char));
if(per[i].name)
{
for(p = buffer; *p != ' '; p++)
per[i].name[j++] = *p;
per[i].name[j] = '\0';
if(sscanf(pointer, "%d", &per[i].grade) != 1)
{
printf("Could not get the grade.\n");
break;
}
per = (Person *)realloc(per, sizeof(Person));
i++;
j = 0;
}
else
{
printf("Memory Error.\n");
break;
}
}
}
if(feof(f))
{
//Return how many persons read.
*Counted = i;
//Free buffers.
free(buffer);
//Close file.
fclose(f);
//Return struct.
return per;
}
else
{
printf("Earlier exit, not all records are read from file %s\n", filename);
return NULL;
}
}
}
}
}
}
//Sort the records according to grade.
//Use of bubble sort here.
void SortPersons(Person *pers , int N)
{
if(pers)
{
int i = 0;
int swapped = 0;
Person per;
memset(&per, 0, sizeof(Person));
do
{
swapped = 0;
for(i = 0; i < N - 2; i++)
{
if(pers[i].grade > pers[i + 1].grade)
{
memcpy(&per, &pers[i], sizeof(Person));
memcpy(&pers[i], &pers[i+1], sizeof(Person));
memcpy(&pers[i+1], &per, sizeof(Person));
swapped = 1;
}
}
}while(swapped);
}
}
int main(int argc, char *argv[])
{
int i;
int n = 0;
Person *persons = GetRecordsFromFile("test.txt", &n);
for(i = 0; i < n; i++)
printf("Person[%d] has name %s and grade %d\n", i, persons[i].name, persons[i].grade);
printf("\n\n\n");
SortPersons(persons, n);
for(i = 0; i < n; i++)
printf("Person[%d] has name %s and grade %d\n", i, persons[i].name, persons[i].grade);
printf("Hit any key to continue...\n");
getch();
return 0;
}
Printed Results.
Person[0] has name nikos and grade 23
Person[1] has name kostas and grade 11
Person[2] has name baggelis and grade 33
Person[3] has name giannis and grade 99
Person[4] has name kadmos and grade 13
Person[5] has name ilias and grade 45
Person[6] has name gionis and grade 67
Person[0] has name kostas and grade 11
Person[1] has name kadmos and grade 13
Person[2] has name nikos and grade 23
Person[3] has name baggelis and grade 33
Person[4] has name ilias and grade 45
Person[5] has name giannis and grade 99
Person[6] has name gionis and grade 67
Hit any key to continue...
//The names are in Greek but it does not matter, create a file and give your names.