Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
void goto_first();
void goto_prev();
void goto_next();
void goto_last();
void menu();
struct stype
{
char s1[100];
char s2[100];
char s3[100];
char s4[100];
};
struct stype studs[10];
FILE *f;
char *t;
char seps[] = ",";
char line[80];
char page[100][80];
int CurrentLine = 0;
int LastLine = 0;
int k;
void breakup(int c)
{
// fill up studs[c]
}
void main()
{
FILE *f;
int len;
int r_num = 1;
int count = 0;
f = fopen("C:\\Students.txt", "r");
if (f !=NULL)
{
printf("\nfile Opened Ok\n");
}
else
{
printf("\nError");
exit(0);
}
while(!feof(f) )
{
for (k=0; k<1; k++)
{
fgets(line, 80,f);
}
if (feof(f))
{
break;
}
len = strlen(line);
if (len > 0)
{
line[len-1] =0;
}
strcpy(page[LastLine++], line);
printf("\n[%s]", line);
}
breakup(count);
count++;
printf("\n\nEND\n");
printf("\n\n");
menu();
}
void menu()
{
char input;
char finish = 0;
do
{
printf("Press, N for Next, P for Previous, F for First,
or L for Last record.\nPress S, to Sort records, by Student #. X to
Exit:\n\n");
input = getch();
switch(input)
{
case 'f':
case 'F':
goto_first();
break;
case 'l':
case 'L':
goto_last();
break;
case 'n':
case 'N':
goto_next();
break;
case 'p':
case 'P':
goto_prev();
break;
case 'x':
case 'X':
finish = 1;
break;
default:
printf("\n\n<WRONG CHAR>\n");
}
} while (finish!=1);
}
void goto_first()
{
CurrentLine = 0;
f = fopen("C:\\Students.txt", "r");
if (f !=NULL)
for (k=0;k<1;k++)
{
fgets(line,80,f);
}
t=strtok(line,seps); strcpy (studs->s1, t);
t=strtok(NULL,seps); strcpy (studs->s2, t);
t=strtok(NULL,seps); strcpy (studs->s3, t);
t=strtok(NULL,seps); strcpy (studs->s4, t);
printf("\n\t ***FIRST RECORD*** ");
printf("\n\nRECORD # %d", k);
printf("\n\n\tSURNAME: \t%s \n\n\tFIRSTNAME: \t%s
\n\n\tSTUDENT #: \t%s\n\n\tD.O.B: \t\t%s\n", studs->s1, studs->s2,
studs->s3, studs->s4);
printf("\n\n");;
printf( "\t ***BEGINNING OF RECORDS*** \n\n");
CurrentLine = 1;
}
void goto_last()
{
CurrentLine = 9;
f = fopen("C:\\Students.txt", "r");
if (f !=NULL)
for (k=0;k<10;k++)
{
fgets(line,80,f);
}
t=strtok(line,seps); strcpy (studs->s1, t);
t=strtok(NULL,seps); strcpy (studs->s2, t);
t=strtok(NULL,seps); strcpy (studs->s3, t);
t=strtok(NULL,seps); strcpy (studs->s4, t);
printf("\n\t ***LAST RECORD*** ");
printf("\n\nRECORD # %d", k);
printf("\n\n\tSURNAME: \t%s \n\n\tFIRSTNAME: \t%s
\n\n\tSTUDENT #: \t%s\n\n\tD.O.B: \t\t%s\n", studs->s1, studs->s2,
studs->s3, studs->s4);
printf("\n\n");;
printf( "\t ***END OF RECORDS*** \n\n");
}
void goto_next()
{
while (CurrentLine != 10)
{
CurrentLine = CurrentLine + 1;
f = fopen("C:\\Students.txt", "r");
if (f !=NULL)
for (k=0;k<CurrentLine;k++)
{
fgets(line,80,f);
}
t=strtok(line,seps); strcpy (studs->s1, t);
t=strtok(NULL,seps); strcpy (studs->s2, t);
t=strtok(NULL,seps); strcpy (studs->s3, t);
t=strtok(NULL,seps); strcpy (studs->s4, t);
printf("\n\t ***NEXT RECORD*** ");
printf("\n\nRECORD # %d", k);
printf("\n\n\tSURNAME: \t%s \n\n\tFIRSTNAME: \t%s
\n\n\tSTUDENT #: \t%s\n\n\tD.O.B: \t\t%s\n", studs->s1, studs->s2,
studs->s3, studs->s4);
printf("\n\n");;
if (CurrentLine >= 10) { printf("\t ***END OF RECORDS***
\n\n");}
return;
}
}
void goto_prev()
{
while (CurrentLine != 1)
{
CurrentLine = CurrentLine - 1;
f = fopen("C:\\Students.txt", "r");
if (f !=NULL)
for (k=0;k<CurrentLine;k++)
{
fgets(line,80,f);
}
t=strtok(line,seps); strcpy (studs->s1, t);
t=strtok(NULL,seps); strcpy (studs->s2, t);
t=strtok(NULL,seps); strcpy (studs->s3, t);
t=strtok(NULL,seps); strcpy (studs->s4, t);
printf("\n\t ***PREVIOUS RECORD*** ");
printf("\n\nRECORD # %d", k);
printf("\n\n\tSURNAME: \t%s \n\n\tFIRSTNAME: \t%s
\n\n\tSTUDENT #: \t%s\n\n\tD.O.B: \t\t%s\n", studs->s1, studs->s2,
studs->s3, studs->s4);
printf("\n\n");;
if (CurrentLine <= 1) { printf("\t ***BEGINNING OF
RECORDS*** \n\n");}
return;
}
}
Apologies if this type of thread isn't allowed.