Right I checked in the file and yes it does have stuff in there. So hers my entrie code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main_menu(); /*main_menu prototype*/
void stop(); /*stop prototype*/
void clrsf(); /*clrsf prototype*/
void print_header(); /*print_header prototype*/
void new_book(); /*new_book prototype*/
void open_book(); /*open_book prototype*/
void view_edit(); /*view_edit prototype*/
int create_entry(); /*create_entry prototype*/
int view_book(); /*view_book prototype*/
void exit_app(); /*exit_app prototype*/
struct data
{
char fname [51],sname [51],tel[15]; /*fname=forename, sname=surename, tel=tel. number.*/
int x; /*x=no. entrys*/
}a;
FILE *f;
char fn[104],y_n;
int i=0;
int t;
/*This creates a pause in the program*/
void stop()
{
system("PAUSE");
}
/*This function clears scanf*/
void clrsf()
{
while ( getchar() != '\n' );
}
/*Program infomation is printed when this function is called*/
void print_header()
{
printf("\nDG's address book\n");
printf("by Daniel Granger\n");
printf("Version 1.0 BETA, released 13/09/02\n");
}
void new_book()
{
char a;
clrscr(); /*clear the screen*/
b1:
printf("What do you want your new address book to be called: "); /*print the text onto the screen*/
scanf("%s", &fn); /*get input from user for address book name*/
a1:
printf("\n\nAre you sure you want to call your new address book '%s'. (y)es or (n)o: ", fn); /*prints text also with the file name in 'fn'*/
clrsf();
scanf("%c", &a); /*gets input from user to see if they want the file name*/
if (a=='y')/*if user typed yes*/
{
strcat( fn, ".dg" ); /*add .dg onto the user's filename*/
stop();
main_menu();
}
else if (a=='n')/*if user typed no*/
{
printf("\nOk then... ");
goto b1; /*up*/
}
else /*if user typed somthing else*/
{
printf("\nIts either y(yes keep the filename) or n(I dont want that file name, let me \nchoose anorther)");
goto a1; /*up*/
}
}
void open_book()
{
char a;
clrscr(); /*clear the screen*/
b2:
printf("Whats the name of the address book you want to open: ");
scanf("%s", &fn);
a2:
printf("\nAre you sure you want to open the address book '%s'. (y)es or (n)o: ");
clrsf();
scanf("%c", &a);
if (a=='y')/*if user typed yes*/
{
strcat( fn, ".dg" );
if ( (f=fopen(fn,"rb"))== NULL)
{
printf("\nUnable to access address book\n");
stop();
fclose(f);
goto b2; /*up*/
}
}
else if (a=='n')/*if user typed no*/
{
printf("\nOk then... ");
goto b2; /*up*/
}
else /*if user typed somthing else*/
{
printf("\nIts either y(yes open that address book) or n(I dont want to open that address \nbook)");
goto a2; /*up*/
}
stop();
main_menu();
}
void view_edit()
{
int a;
clrscr();
printf("Do you want to create a new entry(1) or view the address book(2): ");
scanf("%d", &a);
if (a==1) /* if user typed 1 NEW ENTRY*/
{
create_entry();
}
else if (a==2)
{
view_book();
}
else /*if user doesn't type 1 or 2*/
{
main_menu();
}
}
int create_entry()
{
clrscr(); /*clear the screen*/
printf("\t\t\tCreate Entry\n\n"); /* title*/
a3:
printf("whats the contacts forename: "); /*print text to screen*/
clrsf();
fgets(a.fname, 51, stdin); /*gets contacts forename*/
printf("whats the contacts surname: "); /*print text to screen*/
fgets(a.sname, 51, stdin); /*gets contacts surname*/
printf("whats the contacts telphone number: "); /*print text to screen*/
fgets(a.tel, 15, stdin); /*gets contacts telephone number*/
b3:
printf("\n\nIs the following information correct and do you want to save it?\n"); /*print text to the screen*/
printf("Forename: %s", a.fname); /*print text to the screen with contacts forename*/
printf("Surname: %s", a.sname); /*print text to the screen with contacts surname*/
printf("Telphone number: %s", a.tel); /*print text to the screen with contacts telephone number*/
printf("\n\ny or n: "); /*print text to the screen*/
scanf("%c", &y_n); /*ask yes or no*/
clrsf();
if(y_n=='y')/*yes*/
{
a.x++; /*add one to entrys*/
f=fopen(fn,"a"); /*open file 'fn' and write at last point in the struc*/
if (!f) /*if it can't open the file*/
return 1; /*return 1 and quit*/
fwrite(&a, sizeof(struct data),1,f); /*copy struct data into the binary file*/
fclose(f); /*close file*/
stop();
main_menu();
}
else if (y_n=='n')/*no*/
{
printf("\nOk then... ");
goto a3; /*up*/
}
else /*if user typed anything apart from y or n*/
{
printf("It was y(yes the infomation is correct and you want to save it) or n(no the information is not correct dont save it)");
goto b3; /*up*/
}
}
int view_book()
{
clrscr();
printf("\t\t\tEntry view\n"); /* title*/
f=fopen(fn,"r");
if (!f)
return 1;
t=a.x;
while (i<t)
{
i++;
fread(&a,sizeof(struct data),1,f);
printf("Forename: %s", a.fname); /*print text to the screen with contacts forename*/
printf("Surname: %s", a.sname); /*print text to the screen with contacts surname*/
printf("Telphone number: %s", a.tel); /*print text to the screen with contacts telephone number*/
printf("-__________________________________-\n");
}
fclose(f);
i=0;
stop();
main_menu();
}
void exit_app()
{
clrscr();
printf("\n\n\t\tBYE\n\n");
printf("Thank you for using DG's address book.\n");
stop();
}
/*This is the main menu you get at the beggining of the appilcation*/
void main_menu()
{
int a;
clrscr();
mm_begining:
printf("What do you want to do?\n\n");
printf("1. Create new address book.\n");
printf("2. Open exsiting address book\n");
printf("3. View/edit current book.\n");
printf("4. Exit\n\n");
printf("Please type the number of the options you wish to execute then press enter:-");
scanf("%d", &a);
clrsf();
if (a==1)
new_book();
else if (a==2)
open_book();
else if (a==3)
view_edit();
else if (a==4)
exit_app();
else
{
printf("\nsorry I didn't understand please can you choose an option 1-6\n\n");
goto mm_begining;
}
}
int main()
{
a.x=0;
main_menu();
return 0;
}