Sta je potrebno uraditi na ovome da bi odgovaralo resenju zadatka. Hvala
Code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
char myfile[] = "C:\\myfile.bin";
FILE *pfile = NULL;
struct Date
{
int day;
int month;
int year;
};
typedef struct family
{
struct Date dob;
char name[20];
char pa_name[20];
char ma_name[20];
}Family;
int get_person(Family *pfamily);
void show_person_data(void);
void get_parent_dob(Family *pfamily);
void main()
{
Family member;
Family *pmember = &member;
if((pfile = fopen(myfile, "wb")) == NULL)
{
printf("\nUnable to open %s for writing.\n", myfile);
abort();
}
while(get_person(pmember))
fwrite(pmember, sizeof member, 1, pfile);
fclose(pfile);
show_person_data();
if(remove(myfile))
printf("\nUnable to delete %s.\n", myfile);
else
printf("\nDeleted %s OK.\n", myfile);
}
int get_person( Family *temp)
{
static char more = '\0';
printf("\nDo you want to enter details of a%s person (Y or N)? ",
more != '\0'?"nother " : "" );
scanf(" %c", &more);
if(tolower(more) == 'n')
return 0;
printf("\nEnter the name of the person: ");
scanf("%s", temp->name);
printf("\nEnter %s's date of birth (day month year); ", temp->name);
scanf("%d %d %d", &temp->dob.day, &temp->dob.month, &temp->dob.year);
printf("\nWho is %s's father? ", temp->name);
scanf("%s", temp->pa_name);
printf("\nWho is %s's mother? ", temp->name);
scanf("%s", temp->ma_name);
return 1;
}
void show_person_data(void)
{
Family member;
Family *pmember = &member;
fpos_t current = 0;
pfile = fopen(myfile, "rb");
while(fread(pmember, sizeof member, 1, pfile))
{
fgetpos(pfile, ¤t);
printf("\n\n%s's father is %s, and mother is %s.",
pmember->name, pmember->pa_name, pmember->ma_name);
get_parent_dob(pmember);
fsetpos(pfile, ¤t);
}
fclose(pfile);
}
void get_parent_dob(Family *pmember)
{
Family testmem;
Family *ptestmem = &testmem;
int num_found = 0;
rewind(pfile);
while(fread(ptestmem, sizeof(Family), 1, pfile))
{
if(strcmp(pmember->pa_name, ptestmem->name) == 0)
printf("\n Pa was born on %d/%d/%d.",
ptestmem->dob.day, ptestmem->dob.month, ptestmem->dob.year);
if(++num_found == 2)
return;
}
else
if(strcmp(pmember->ma_name, ptestmem->name) == 0)
{
printf("\n Ma was born on %d/%d/%d.",
ptestmem->dob.day, ptestmem->dob.month, ptestmem->dob.year);
if(++num_found == 2)
return;
}
}
}
[Ovu poruku je menjao X Files dana 16.07.2006. u 20:29 GMT+1]