Code:
#include <string.h>
#include <stdio.h>
char seps[] = " , ";
char *token;
typedef enum Menu { FIRST = 1, NEXT, PREVIOUS, LAST, SORT, EXIT }; // Define the menu
typedef struct // Define the student structure
{
char fName[10];
char lName[10];
char regNo[10];
char doB[30];} PERSON;
PERSON student[11];
EnterChoice(); // display main menu
void firstRec(); // view First record
void nextRec(); // view Next record
void previousRec(); // view Previous record
void lastRec(); // view Last record
void sortRec(); // Sort records by Registration Number
char choice;
int i;
int counter;
//-----------------------------------------------------------------------------------
int main()
{
char s[100];
FILE * f;
int len, i;
i=-1;
f = fopen("c:\\students.txt","r");
for ( ; ; ) {
fgets(s, 100, f);
if feof(f) break;
len = strlen(s);
if (len) { s[len] = 0;}
if (i>=9) break;
i++;
{
token = strtok( s, seps ); strcpy(student[i].fName, token);
token = strtok( NULL, seps ); strcpy(student[i].lName, token);
token = strtok( NULL, seps ); strcpy(student[i].regNo, token);
token = strtok( NULL, seps ); strcpy(student[i].doB, token);
}
}
fclose(f);
while ( ( choice = EnterChoice() ) != EXIT ) // menu loop
{
switch ( choice )
{
case FIRST: firstRec(); break;
case NEXT: nextRec(); break;
case PREVIOUS: previousRec(); break;
case LAST: lastRec(); break;
case SORT: sortRec(); break;
}
}
}
//------------------------------------------------------------------------------------
EnterChoice() // Menu function
{
printf( "\nEnter your choice\n"
"1 - View first record\n"
"2 - View next record\n"
"3 - View previous record\n"
"4 - View last record\n"
"5 - Sort records by Registration Number\n"
"6 - End program\n? " );
scanf( "%d", &choice );
return choice;
}
void firstRec() // View the first record
{
counter=0;
printf("\nFirst Name: %s\n\n", student[counter].fName);
printf("Last Name: %s\n\n", student[counter].lName);
printf("Registration Number: %s\n\n", student[counter].regNo);
printf("Date of Birth: %s\n", student[counter].doB);
}
void nextRec() // View the next record
{
++counter;
if (counter >=10)
{
printf("\nThere are no more records to view!\n\n");
}
else
{
printf("\nFirst Name: %s\n\n", student[counter].fName);
printf("Last Name: %s\n\n", student[counter].lName);
printf("Registration Number: %s\n\n", student[counter].regNo);
printf("Date of Birth: %s\n", student[counter].doB);
}
}
void previousRec() // View the previous record
{
--counter;
if (counter <=-1)
{
printf("\nThere are no more records to view!\n\n");
}
else
{
printf("\nFirst Name: %s\n\n", student[counter].fName);
printf("Last Name: %s\n\n", student[counter].lName);
printf("Registration Number: %s\n\n", student[counter].regNo);
printf("Date of Birth: %s\n", student[counter].doB);
}
}
void lastRec() // View the last record
{
counter=9;
printf("\nFirst Name: %s\n\n", student[counter].fName);
printf("Last Name: %s\n\n", student[counter].lName);
printf("Registration Number: %s\n\n", student[counter].regNo);
printf("Date of Birth: %s\n", student[counter].doB);
}
void sortRec() // Sort records by Registration Number
{
int i,j;
PERSON tp;
printf("\nRecords now sorted by Registration Number.\n\n");
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(strcmp(student[i].regNo,student[j].regNo) > 0)
{
tp=student[i];
student[i]=student[j];
student[j]=tp;
}
}
}
printf("\nFirst Name Last Name Registration Number DOB\n");
for(i=0;i<10;i++)
{
printf("%s ", student[i].fName);
printf("%s ", student[i].lName);
printf("%s ", student[i].regNo);
printf("%s", student[i].doB);
}
}