home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / c / cnews019.zip / FONENUM.C < prev   
C/C++ Source or Header  |  1990-08-05  |  4KB  |  115 lines

  1. /* FONENUM.C searches a file for a phone number using only */
  2. /* the (supplied) lastname, and also allows additions of   */
  3. /* names to the file when "enter" is the lastname supplied */
  4.  
  5. /* This is deliberately written with function declarations */
  6. /* preceding function use, for strict ANSI argument checks */
  7. /* and no need of a header file.  Things are defined in    */
  8. /* one place only.  Globals are carefully minimized.       */
  9.  
  10. /* My formatting philosophy is to keep all of a function   */
  11. /* on a single page and to have that deal with one thought */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. #define MAXLASTNM   30
  17. #define MAXFIRSTNM  15
  18. #define MAXFONENUM  12
  19. #define DATABASE    "input"
  20.  
  21. struct record {
  22.   char lastname[MAXLASTNM + 1];
  23.   char firstname[MAXFIRSTNM + 1];
  24.   char phonenum[MAXFONENUM + 1];
  25.   };
  26.  
  27. /* ------------------------------------ */
  28. /* emit program calling syntax verbiage */
  29. void
  30. help(void) {
  31.  
  32.   puts("Usage:  FONENUM <name> ");
  33.   puts(" Where <name> is the lastname of the person.");
  34.   puts(" In order to add names to the file, enter the");
  35.   puts(" word 'Enter' as the name of the person.");
  36.   } /* help */
  37.  
  38. /* ----------------------------------------- */
  39. /* prompt and get input string, limit length */
  40. void
  41. promptget(char *prompt, char *answer, int maxlgh) {
  42.   int i;
  43.  
  44.   printf(prompt); fflush(stdin);  /* reverse order ?? */
  45.   fgets(answer, maxlgh, stdin);
  46.  
  47.   /* There MUST be clearer and better code for the rest */
  48.   if (answer[strlen(answer)-1] == '\n')   /* remove any trailing \n */
  49.     answer[strlen(answer)-1] = 0;
  50.   for (i = strlen(answer); i <= maxlgh; i++) {      /* clean string */
  51.     answer[i] = 0;
  52.     }
  53.   } /* promptget */
  54.  
  55. /* ---------------------------------------------- */
  56. /* get record data from stdin and add to database */
  57. void
  58. addrecords(FILE *fptr) {
  59.   struct record currentitem;
  60.  
  61.   promptget("Enter last name:  ", currentitem.lastname, MAXLASTNM);
  62.   promptget("Enter first name: ", currentitem.firstname, MAXFIRSTNM);
  63.   promptget("Phone number (XXX-XXX-XXXX):  ", currentitem.phonenum,
  64.              MAXFONENUM);
  65.   if (0 == fwrite(¤titem, sizeof(currentitem), 1, fptr)) {
  66.     puts("Write failure");
  67.     }
  68.   } /* addrecords */
  69.  
  70.  
  71. /* ---------------------------------------------- */
  72. /* display all database records matching searchee */
  73. void
  74. displaymatches(FILE *fptr, char *searchee) {
  75.   struct record currentitem;
  76.   int           names, count;
  77.  
  78.   names = count = 0;
  79.   while (0 != fread(¤titem, sizeof(currentitem), 1, fptr)) {
  80.     if (0 == strcmpi(currentitem.lastname, searchee)) {
  81.       printf("\n%s %s:  %s", currentitem.firstname,
  82.                              currentitem.lastname,
  83.                              currentitem.phonenum);
  84.       names++;
  85.       }
  86.     count++;
  87.     }
  88.   } /* displaymatches */
  89.  
  90.  
  91. /* ---------------------------------- */
  92. /* Main body and overall flow control */
  93. void
  94. main(int argc, char *argv[]) {
  95.   FILE *fptr;
  96.  
  97.   if (argc != 2) help();            /* extra or no params mean misentry */
  98.   else if (0 == strcmpi("enter", argv[1])) {                 /* loading */
  99.     if (NULL == (fptr = fopen(DATABASE, "ab"))) {
  100.       puts("Cannot open file for storing data.");
  101.       }
  102.     else {                                           /* successful open */
  103.       addrecords(fptr);
  104.       fclose(fptr);
  105.       }
  106.     }
  107.   else if (NULL == (fptr = fopen(DATABASE, "rb"))) {       /* searching */
  108.     puts("Cannot open file for reading data.");
  109.     }
  110.   else {                                             /* successful open */
  111.     displaymatches(fptr, argv[1]);
  112.     fclose(fptr);
  113.     }
  114.   } /* main */
  115.