This is a program I wrote a long time ago in C. It's not a solution but it should prove fairly helpful.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct {
char surname[15];
char forename[15];
char telephone[15];
}PERSON;
PERSON person;
void main(void)
{
void write_names(void), read_names(void);
char reply;
do
{
clrscr();
printf("\t\t\tElectronic Address Book\n\n");
printf("1. Add a name\n");
printf("2. List all names\n");
printf("3. Quit\n");
reply = getchar();
fflush(stdin);
switch(reply)
{
case '1' : write_names();
break;
case '2' : read_names();
}
}
while (reply != '3');
}
void write_names(void)
{
FILE *fp;
if((fp = fopen("names.dat","ab")) == NULL)
printf("Unable to open file!");
else
{
printf("Please enter surname : ");
gets(person.surname);
printf("Please enter forname : ");
gets(person.forename);
printf("Please enter telephone number : ");
gets(person.telephone);
fwrite(&person,sizeof(PERSON),1,fp);
fclose(fp);
}
}
void read_names(void)
{
FILE *fp;
int result;
clrscr();
if((fp = fopen("names.dat","rb")) == NULL)
printf("Unable to find file");
else
{
while(!feof(fp))
{
result = fread(&person,sizeof(PERSON),1,fp);
if (result != NULL)
printf("%s %s Telephone : %s\n",person.forename,person.surname,person.telephone);
}
fclose(fp);
printf("\n\nPress ENTER to continue");
getchar();
fflush(stdin);
}
}