/* person.c */ #include #include #include #include #define PERSON_INIT #include "person.h" #include "menu.h" /*---------------------------------------------------------- copyPerson() this function copies the contents of p1 into p2. ----------------------------------------------------------*/ void copyPerson( PERSON_TYPE *p1, PERSON_TYPE *p2 ) { p2->number = p1->number; strcpy( p2->lastname,p1->lastname ); strcpy( p2->firstname,p1->firstname ); strcpy( p2->phone,p1->phone ); } /* end of copyPerson() */ /*---------------------------------------------------------- readField() ----------------------------------------------------------*/ void readField( char **field, int max, char *prompt ) { char *p = (char *)malloc( (max+3)*sizeof( char )); printf( "%s",prompt ); p = fgets( p,max+2,stdin ); *field = strip( p ); } /* end of readField() */ /*---------------------------------------------------------- readPerson() this function prompts the user and reads data from the keyboard and stores it in a PERSON_TYPE structure. it allocates storage for one PERSON_TYPE structure and returns a pointer to that structure. ----------------------------------------------------------*/ PERSON_TYPE *readPerson() { PERSON_TYPE *tmp = (PERSON_TYPE *)malloc( sizeof( PERSON_TYPE )); readField( &(tmp->lastname),50,"enter last name: " ); readField( &(tmp->firstname),50,"enter first name: " ); readField( &(tmp->phone),10,"enter phone (10 digits, no spaces or punctuation): " ); tmp->number = ++lastperson; return( tmp ); } /* end of readPerson() */ /*---------------------------------------------------------- printPerson() this function displays on the screen the content of the PERSON_TYPE structure argument. ----------------------------------------------------------*/ void printPerson( PERSON_TYPE *person ) { printf( "#%04d [%s,%s (%3.3s)%3.3s-%4.4s]\n", person->number, person->lastname, person->firstname, &(person->phone[0]),&(person->phone[3]), &(person->phone[6]) ); } /* end of printPerson() */ /*---------------------------------------------------------- writePersonFile() this function writes the contents of the "people" global array into a data file, which is named according to the function argument. ----------------------------------------------------------*/ void writePersonFile( char *filename ) { int i; /* open data file for writing (which will erase any contents that might exist in that file */ FILE *fp = fopen( filename,"w" ); if ( fp == NULL ) { fprintf( stderr,"error opening file: %s %s\n", filename,strerror(errno)); return; } /* write lastperson to the file first */ fprintf( fp,"%d\n",lastperson ); /* loop through the array, printing each PERSON_TYPE record */ for ( i=0; inumber,people[i]->lastname, people[i]->firstname,people[i]->phone); } /* close the data file */ fclose( fp ); } /* end of writePersonFile() */ /*---------------------------------------------------------- readPersonFile() this function reads the contents of a data file, which is named according to the function argument and stores the contents in the "people" global array. ----------------------------------------------------------*/ void readPersonFile( char *filename ) { int more = 1; FILE *fp; char *p; PERSON_TYPE *tmp; char *line = (char *)malloc( 1024*sizeof( char )); /* open the data file for reading */ fp = fopen( filename,"r" ); if ( fp == NULL ) { fprintf( stderr,"error opening file: %s %s\n", filename,strerror(errno)); return; } /* read lastperson from the file first */ fscanf( fp,"%d",&lastperson ); /* loop through the data file, reading one record at a time and storing each record in the "people" global array */ while( more ) { tmp = (PERSON_TYPE *)malloc( sizeof( PERSON_TYPE )); fscanf( fp,"%d",&(tmp->number)); line = fgets( line,1024,fp ); if ( line == NULL ) { more = 0; } // end if else { line = strip( line ); tmp->lastname = (char *)malloc( 51*sizeof( char )); tmp->firstname = (char *)malloc( 51*sizeof( char )); tmp->phone = (char *)malloc( 11*sizeof( char )); strcpy( tmp->lastname, strtok( line,"|" )); strcpy( tmp->firstname, strtok( NULL,"|" )); strcpy( tmp->phone, strtok( NULL,"|" )); people = (PERSON_TYPE **)realloc( people,(npeople+1)*sizeof( PERSON_TYPE * )); people[npeople] = tmp; printPerson( people[npeople] ); npeople++; } // end else } // end while /* close data file */ fclose( fp ); } /* end of readPersonFile() */