Sams Teach Yourself C in 21 Days

(singke) #1
624 Week 3

196: * Function: look_up()
197: * Returns: Number of names matched
198: *************************************************/
199:
200: int look_up( FILE *fp )
201: {
202: char tmp_lname[20+1];
203: int ctr = 0;
204:
205: fprintf(stdout, “\n\nPlease enter last name to be found: “);
206: gets(tmp_lname);
207:
CH 17 208: if( strlen(tmp_lname) != 0 )
209: {
CH 16 210: if (fseek( fp, 0, SEEK_SET ) == 0)
211: {
CH 16 212: fread(&rec, 1, sizeof(rec), fp);
213: while( !feof(fp))
214: {
CH 17 215: if( strcmp(rec.lname, tmp_lname) == 0 )
216: /* if matched */
217: {
218: fprintf(stdout, “\n%s %s %s - %s”, rec.fname,
219: rec.mname,
220: rec.lname,
221: rec.phone);
222: ctr++;
223: }
224: fread(&rec, 1, sizeof(rec), fp);
225: }
226: }
227: fprintf( stdout, “\n\n%d names matched.”, ctr );
228: }
229: else
230: {
231: fprintf( stdout, “\nNo name entered.” );
232: }
233: return(ctr);
234: }

LISTINGR3.1 continued

This program is similar in some respects to the programs presented in Week 1 in
Review and Week 2 in Review. Fewer data items are tracked, but additional pro-
gram capabilities have been added. week3.c lets the user keep track of names and phone
numbers of friends, business contacts, and so on. As written, the program tracks only
first name, last name, middle name, and phone number. It would be a simple matter to
have the program record additional information, and you might want to try this as an
exercise. The major difference between this program and the earlier review programs is

ANALYSIS

34 448201x-W3R 8/13/02 11:22 AM Page 624

Free download pdf