How to find a string in a struct..
I'm making a database for fx a school soccerteam .
When I have loaded all the file info. into the struct, I need to make it possible to seek for players..
If i make a routine like: please input the players you want fx sorted after shirtnumbers..input: 10.
Then the program should printf out the info about shirt no. 10.
fx:
10, David Madsen, Birthday: 12.02.1978 etc.
Can anybody help me with this. that would mean alot to me..tx..
I trying something with read(handle,...,...), but it don't seems to work prober..Also strstr(..) has been around my desktop..
Tx in advance
!G!
------------my kode so far------------cut..
#define FILENAME "datafile.txt"
#define ENTER_PLAYERSx
#define GET_PLAYERS
#ifdef ENTER_PLAYERS
/* Record of data */
struct data
{
char fname[30]; /* First name */
char lname[30]; /* Last name */
char shirtnr[3]; /* Shirtnumber */
int birthday[12];
}rec[30];
/*----Input Players to the Database.----*/
void input_players(void)
{
FILE *input;
int entries = 0;
int loop;
printf("How many Players will you type in?: ");
scanf("%d", &entries);
fflush(stdin);
for(loop=0; loop<entries;++loop)
{
printf("\nEntry: %d\n", loop);
printf("Enter the players Shirtnumber: ");
scanf("%s", rec[loop].shirtnr);
fflush(stdin);
printf("\nEnter the player's first and last namename, \n");
printf("separated by a space: ");
scanf("%s %s", rec[loop].fname, rec[loop].lname);
fflush(stdin);
printf("Enter the players birthday <ex. 15.03.1974>: ");
scanf("%s", rec[loop].birthday);
fflush(stdin);
}
/* White the data to a file */
input = fopen (FILENAME, "a+");
for(loop=0; loop<entries;++loop)
{
fprintf(input, "%2s %2s, Shirtnumber: %2s, Birthday: %2s\n",
rec[loop].fname,
rec[loop].lname,
rec[loop].shirtnr,
rec[loop].birthday);
}
/* display the information */
for(loop=0; loop<entries;++loop)
{
printf("\n");
printf("%2s %2s Shirtnumber: %2s Birthday: %2s",
/* loop + 1,*/
rec[loop].fname,
rec[loop].lname,
rec[loop].shirtnr,
rec[loop].birthday);
}
}
#endif
void input_players(void);
int main()
{
/* Buffer to hold one single line from the file, the size is declared as
BUFSIZE, because one line of the file will rarely if even go above 256 ch. */
char buffer[BUFSIZ];
FILE *output;
struct indata
{
char name[BUFSIZ]; /* name */
int shirtnr; /* Shirtnumber */
int birthday[12];
}record;
/*input_players();*/
if ((output = fopen(FILENAME, "r")) !=NULL)
{
while(fgets(buffer,(int)sizeof buffer, output) !=NULL)
{
if(sscanf(buffer, "%d,%[^,],%[^,]", &record.shirtnr, &record.name,
&record.birthday) == 3)
printf("Shirtnr: %2d, Name: %20s, Birthday: %10s\n", record.shirtnr,
record.name,
record.birthday);
else
{
printf("\n\nERROR - Invalid Input\n\n");
getch();
return 0;
}
}
}
getch();
return 0;
}
----
The datafile.txt might be as follows:
10, Gustaf Norgaard, 15.03.1974
9, Sara Kistrup, 03.06.1977
8, Mikkel jensen, 25.03.1974
7, Kenneth Mikkelsen, 12.06.1977
6, Hans Niel, 06.03.1973
5, Kasper Hansen, 17.06.1980e.t.c