Sorry about the long post, but newbie here.
I've been creating a Database in text format, which is so far going good.
Everything i've done so far, is working fine and i'm happy with it.
The database loads, i can navigate through the files next, previous, first, last.
But, i need to be able to sort, using qsort, the files by ID#. I was wondering if anyone could help, as i'm stuck.
I've got a bit of coding from books & web, but can't get it to work.
There are 10 files (records), in the format surname, firstname, id#, DOB.
I would like to sort all 10 records via the id#, which is of the format 00000000, where the first 2 digits are the year. 01 = 2001, 99 = 1999.
Also would like the other details, name, etc displayed on the same line, but after the id#.
I would like to keep the code i have already, which i will provide below.
Could someone please help me with the code to use. It should work under void goto_sort, S key.
I hope its not too much to ask.
As i can't get the sort coding working from what i already have, i have removed this from the code.
Thanks.
Apologies if this type of thread isn't allowed.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;
}
}