/* CIP 247 C programming Project Option 2 Address Book This program maintains an address book */ #include #include #include #define MAX 1000 struct record { /* declare structure */ char name[50]; char phone[15]; char address[10]; char street[20]; char city[50]; char state[20]; char zip[12]; } ; int menu(void); void add(void); void deleteIn(void); void deleteRecord(void); void edit(void); void list(void); int find(void); void findisplay(void); void openFile(void); void closeFile(void); void readFromDisk(void); void writeToDisk(void); /*************************/ void storeCount(void); void loadCount(void); void updateFile(void); void loadFile(void); /*************************/ struct record phoneBook[MAX]; /* declare array of structures */ struct record temp; struct record temp2; FILE *fp; char SK[81]; int count =0; int i, found; int main() { int choice; /* try to open the addresses.bin file. We open it for read-write. */ fp= fopen("addresses.bin" , "rb+" ); if ( fp == NULL ) { /* We didn't find a file, let's create one */ printf("Cannot find address file\n\n"); printf("Creating new address file\n"); fp = fopen( "addresses.bin" , "wb+" ); system("PAUSE"); if (fp == NULL ) { printf( "Could not create address file\n" ); system("PAUSE"); } } closeFile(); loadFile(); /* <-- brs */ do { choice = menu(); switch(choice) { case 1: /* "add" menu-option */ if(count :"); scanf("%d",&choice); return choice; } void add(void ) { int compare; puts("Enter the name using last-name first-name format: "); fflush(stdin); gets(temp.name); /* CHECK FOR DUPLICATE RECORD - STARTS HERE */ found = find(); /* find() FUNCTION LOOKS FOR RECORD BETWEEN 1 AND COUNT */ if( found == 0 ) { puts("\n\nENTRY ALREADY EXISTS.\n "); system("PAUSE"); return; } /* CHECK FOR DUPLICATE RECORD END */ puts("\n\nEnter the phone number , using the format (xxx)xxx-xxxx"); puts("\nbeginning with the area code. \n"); fflush(stdin); gets(temp.phone); puts("Enter the address (house ,apt.or PO#):"); fflush(stdin); gets(temp.address); puts("Enter the Street name:"); fflush(stdin); gets(temp.street); puts("Enter the City: (First letter Upper-case)"); fflush(stdin); gets(temp.city); puts("Enter the State: (first letter upper-case)"); fflush(stdin); gets(temp.state); puts("Enter the Zip Code:"); fflush(stdin); gets(temp.zip); strcpy(SK, temp.name); /*SK = temp.name; /* move contents of name element into Search Key */ /*printf("searchkey = %s",SK); /* for debugging */ /* SECTION TO SORT THE RECORD ALPHABETICALLY AS IT MOVES FROM TEMP TO STRUCTURE STARTS HERE*/ for(i=1; i <= count; i++) if((compare=strcmp(phoneBook[i].name,temp.name)) >=0) { printf("compare is %d in sort loop",compare); temp2 = phoneBook[i]; phoneBook[i] = temp; temp = temp2; } count++; phoneBook[count] = temp; //openFile(); /* <-- brs */ /*writeToDisk(); */ //closeFile(); /* <-- brs */ updateFile(); /* <-- brs */ return ; } int find(void) { found = 1; /*openFile(); rewind(fp); readFromDisk(); closeFile(); */ /*printf("\nhello from find function, before the for loop"); */ for(i=1;i<=count;i++) { if((strcmp(temp.name,phoneBook[i].name))==0) { /* printf("\nfound in find loop = %d i=%d",found,i); */ return found= 0; } } /* printf("\nfound (after find loop) = %d",found); */ return found; } void findisplay(void) { printf("Name: %s\n",phoneBook[i].name); printf("Phone: %s\n",phoneBook[i].phone); printf("Address: %s - %s\n",phoneBook[i].address, phoneBook[i].street); printf("City: %s\n",phoneBook[i].city); printf("State: %s\n",phoneBook[i].state); printf("Zip: %s\n\n",phoneBook[i].zip); } void list(void) { /*openFile(); rewind(fp); readFromDisk(); closeFile(); */ for(i=1; i < (count+1); ++i) { printf("Name: %s\n",phoneBook[i].name); printf("Phone: %s\n",phoneBook[i].phone); printf("Address: %s - %s\n",phoneBook[i].address, phoneBook[i].street); printf("City: %s\n",phoneBook[i].city); printf("State: %s\n",phoneBook[i].state); printf("Zip: %s\n\n",phoneBook[i].zip); /* printf("count = %d\n",count); /*debug aid*/ } system("PAUSE"); return; } void edit(void) { char lookup[50]; int choice; openFile(); rewind(fp); readFromDisk; closeFile(); fflush(stdin); gets(lookup); for(i=1;i<=count;i++) { if( strcmp(lookup,phoneBook[i].name)==0) { /* printf("\nlookup in edit loop = %s i=%d\n",lookup,i); */ printf("name: %s found in database\n",phoneBook[i].name); do { system("cls"); printf("Select field to edit:\n\n"); printf("\t1:phone\n"); printf("\t2:address\n"); printf("\t3:street\n"); printf("\t4:city\n"); printf("\t5:state\n"); printf("\t6:zip\n"); printf("\t7: Return to main menu\n\n\n"); scanf("%d",&choice); switch(choice) { case 1: printf("phone: %s\n",phoneBook[i].phone); printf("Enter new phone #:\n"); fflush(stdin); gets(phoneBook[i].phone); break; case 2: printf("address: %s\n",phoneBook[i].address); printf("Enter new house, apt or PO#:\n"); fflush(stdin); gets(phoneBook[i].address); break; case 3: printf("street: %s\n",phoneBook[i].street); printf("Enter new street #:\n"); fflush(stdin); gets(phoneBook[i].street); break; case 4: printf("city: %s\n",phoneBook[i].city); printf("Enter new city:\n"); fflush(stdin); gets(phoneBook[i].city); break; case 5: printf("state: %s\n",phoneBook[i].state); printf("Enter new state:\n"); fflush(stdin); gets(phoneBook[i].state); break; case 6: printf("zip: %s\n",phoneBook[i].zip); printf("Enter new zip:\n"); fflush(stdin); gets(phoneBook[i].zip); break; case 7: break; default: printf("Invalid selection\n\n"); } }while((choice >0)&&(choice <7)); return; } /* bracket closes if statement */ } /* bracket to close for loop */ printf("\nName not found"); printf("\nReturning to main menu......................\n"); system("PAUSE"); return; } /* bracket to close edit function */ void deleteIn(void) { char ch; openFile(); rewind(fp); readFromDisk; closeFile(); printf("\nEnter name of record to delete:\n"); fflush(stdin); gets(temp.name); find(); if(found == 0) printf("\nRecord found\n"); else { printf("%s not found in database\n",temp.name); printf("\nReturning to main menu....................\n\n"); system("PAUSE"); return; } do { printf("Are you sure you want to delete %s?\n",temp.name); printf("Y=yes N=no\n\n"); scanf("%c",&ch); switch(ch) { case 'y': case 'Y': deleteRecord(); break; case 'n': case 'N': printf("Returning to main menu..........\n"); break; default: printf("INVALID SELECTION, Must be Y or N\n"); } }while((ch !='y' )/* || (ch!='Y')) */&& (ch !='n')/*||(ch!='N'))*/); system("PAUSE"); openFile(); writeToDisk(); closeFile(); return; } void deleteRecord(void) { int j; /*printf("i =%d\n",i); */ for(j=i;j<=(count-1); j++) { /*printf("j=%d",j); */ phoneBook[j] = phoneBook[j+1]; } /* phoneBook[count].name = ; */ count = (count-1); printf("\nRecord deleted\n"); openFile(); writeToDisk(); closeFile(); return; } void openFile(void) { if((fp=fopen("addresses.bin", "rb+")) == NULL) { fprintf(stderr, "Failed to open file: addresses.bin\n"); exit(1); } return; } void closeFile(void) { fclose(fp); return; } void writeToDisk() { fwrite(&phoneBook, sizeof(phoneBook),1,fp); return; } void readFromDisk(void) { int n =1; while(fread(&phoneBook[n], sizeof(phoneBook[n]),1,fp)==1) n++; return; } /*************************************************/ void storeCount(void) { itoa(count, phoneBook[0].zip, 10); printf( "count = %s", phoneBook[0].zip); } void loadCount(void) { count = atoi(phoneBook[0].zip); } void updateFile(void) { int j; int recLength; /* THIS FUNCTION LACKS ERROR-CHECKING ! */ /* OPEN FILE */ openFile(); /* CALCULATE REC LENGTH */ recLength = sizeof(phoneBook[0]) ; /* STORE RECORD COUNT IN HEADER RECORD */ storeCount(); /* WRITE RECORDS INTO ARRAY REC-BY-REC */ for(j=0; j<=count; j++) fwrite(&phoneBook[j], recLength, 1, fp); /* CLOSE FILE */ closeFile(); } void loadFile(void) { int j; int recLength; /* THIS FUNCTION LACKS ERROR-CHECKING ! */ /* OPEN FILE */ openFile(); /* CALCULATE REC LENGTH */ recLength = sizeof(phoneBook[0]) ; /* READ HEADER REC TO GET RECORD COUNT */ fread(&phoneBook[0], recLength, 1, fp); /* LOAD RECORD COUNT FROM HEADER REC INTO VARIABLE count */ loadCount(); /* READ RECORDS INTO ARRAY REC-BY-REC */ for(j=1; j<=count; j++) fread(&phoneBook[j], recLength, 1, fp); /* CLOSE FILE */ closeFile(); }