/************************************************************ * * phone.c * * The routines in this file maintain the phone book data * structure. All access to this data structure must be * through the routines in this file. * **********************************************************/ #include #include #include "phone.h" /* * SIZE is the length of the array used to store the * phone list */ #define SIZE 200 /* * phone_table is the array that stores the phone list. * Numbers is the number of entries in the phone list. */ static struct phone_struct phone_table[SIZE]; static int Numbers = 0; /* * read_phone_list * * Purpose: transfer the phone list from disk to internal storage * * Pre-conditions: the phone list is initially empty * * Post-conditions: the phone_table array has the contents of the * disk file */ void read_phone_file (FILE* fid) { int n; int i; i = 0; while (TRUE) { n = fscanf(fid,"%20s %10s", &(phone_table[i].name[0]), &(phone_table[i].phone)[0]); if (n < 0) break; i++; } Numbers = i; } /* * get_phone_number * * Purpose: look up the name corresponding to a phone number * * Pre-conditions: the name is in the phone list * * Post-conditions: returns the number corresponding to the * name that was passed as a parameter */ char* get_phone_number (char* name) { int i; for (i = 0; i < Numbers; i++) { if ( strcmp (name, phone_table[i].name) == 0 ) { return (phone_table[i].phone); } } return (NULL); } /*------------------end of phone.c----------------*/