Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <ctype.h>
#include <string.h>
#define NAME_SIZE 10
#define ADD_SIZE 100
#define PH_SIZE 15
#define CLEAN_BUFF while ( getchar() != '\n')
void drawscreen ( void );
void refreshscreen( void );
char menu( void );
void createdb( FILE ** , int *, char []);
void delrecord( FILE * );
struct data{
char firstname[NAME_SIZE];
char lastname[NAME_SIZE];
char address[ADD_SIZE];
char phone[PH_SIZE];
};
char dbload[80];
int main()
{
char select;
FILE *fPtr;
char filename[PH_SIZE];
int tf = 0;
struct data Data;
strcpy( dbload,"No file loaded.." );
while ( select != 'e'){
select = menu();
if ( select == 'a')
{
createdb( &fPtr, &tf, filename );
if ( tf == 0 )
{
gotoxy(1,25);
cprintf("Create a file or Load a file before doing anything" );
getchar();
}
else{
strcpy( dbload,"File loaded..." );
while( 1 ){
refreshscreen();
drawscreen();
fflush( stdin );
gotoxy(1,5);
printf("Enter the infomation below(press <return> to go back).\n" );
gotoxy(1,7);
printf("Enter person First name: " );
fgets(Data.firstname, sizeof(Data.firstname), stdin );
if ( Data.firstname[0] == '\n' )
{
gotoxy(1,25);
cprintf("You wish to exit.");
getchar();
break;
}
printf("Enter persons Surname: " );
fgets(Data.lastname, sizeof(Data.lastname), stdin );
printf("\n");
printf("Enter his Address: ");
fgets(Data.address, sizeof(Data.address), stdin);
printf("\n");
printf("Enter his phone number: ");
fgets(Data.phone, sizeof(Data.phone), stdin );
fprintf(fPtr, "%s %s %s %s", Data.firstname, Data.lastname, Data.address,Data.phone);
}
}
}
}
return 0;
}
void drawscreen ( void )
{
gotoxy(1,1);
cprintf("-------------------------------------------------------------------------------");
gotoxy(1,2);
cprintf(" *~*~*~* Address Book v1.0 *~*~*~* ");
gotoxy(1,3);
cprintf("-------------------------------------------------------------------------------");
}
void refreshscreen ( void )
{
clrscr();
textcolor(WHITE);
textbackground(BLACK);
gotoxy(1,25);
cprintf(" ");
clrscr();
textcolor(WHITE);
textbackground(BLUE);
gotoxy(1,25);
cprintf(" ");
gotoxy(1,4);
}
char menu ( void )
{
int choice;
window(1,1,80,25); /*Set position and screen mode*/
refreshscreen();
drawscreen();
gotoxy(1,5);
printf( "[A]dd an Entry.\n" );
printf( "[D]elete an Entry.\n" );
printf( "[U]pdate an Entry.\n" );
printf( "[S]earch an Entry.\n" );
printf( "[L]ist all Entry.\n" );
printf( "[M]ake a printing version of the Entry.\n" );
printf( "[C]reat a Database File.\n" );
printf( "[F]ile Load.\n" );
printf( "[H]elp.\n" );
printf( "[E]xit.\n" );
gotoxy(1,25);
cprintf("Please select an option from the above menu." );
gotoxy(1,17);
printf("File Status : %s", dbload );
gotoxy(1,16);
printf( "Select an option:" );
choice = tolower(getch());
return choice;
}
void createdb( FILE **cfPtr, int *filestat,char filename[PH_SIZE] )
{
int i;
char *p;
refreshscreen();
drawscreen();
gotoxy(1,25);
cprintf("Creating a Database file..." );
gotoxy(1,6);
printf("Example( C:\\myaddress.txt ). ");
gotoxy(1,5);
printf("Enter the name of the file to create: ");
fgets( filename, sizeof filename, stdin );
if ( ( p = strchr(filename, '\n' )) != NULL ){
*p = '\0';
}
else{
while( getchar() != '\n');
}
if ( (*cfPtr = fopen( filename,"w" )) == NULL )
{
gotoxy(1,25);
cprintf( "ERROR : Could not create file.." );
getchar();
}
else
*filestat = 1;
}