home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CNEWS018.ZIP / PHONENUM.ZIP / PHONENUM.C next >
Text File  |  1989-12-17  |  2KB  |  106 lines

  1. /*  PHONENUM.C -- Searches a file for a phone number using a person's lastname.
  2.     also allows new names to be added by passing "Enter" as the lastname.  */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. /*  Create a structure template for the structure with the tag "record".  */
  9.  
  10. struct record {
  11.         char lastname[31];
  12.         char firstname[16];
  13.         char phonenum[13];
  14.           };
  15.  
  16. FILE *fptr;
  17.  
  18. void main(int argc, char *argv[])
  19. {
  20.     struct record inputt;
  21.     int count = 0;
  22.     int names = 0;
  23.  
  24.     /*  Check to make sure enough arguements were passed.  If not, display
  25.     an error message.  */
  26.     
  27.     if(argc == 1)
  28.     {
  29.         puts("\nUsage:  PHONENUM <name> ");
  30.         puts("\nWhere <name> is the lastname of the person.");
  31.         puts("In order to add names to the file, enter the");
  32.         puts("word 'Enter' as the name of the person.");
  33.         exit(1);
  34.     }
  35.     
  36.     /*  If "Enter" was entered as the lastname, allow user to add a record.  */
  37.  
  38.     if(strcmpi("enter", argv[1]) == 0)
  39.     {
  40.  
  41.         /*  Open the file for storing the entered data.  */
  42.  
  43.         if((fptr=fopen("input","ab")) == NULL)
  44.         {
  45.             printf("\nCannot open file for storing data.");
  46.             exit(1);
  47.         }
  48.  
  49.         /*  Get the input data from the user and write it to the data file.  */
  50.  
  51.         printf("\nEnter last name:  ");
  52.         gets(inputt.lastname);
  53.  
  54.         printf("\nEnter first name:  ");
  55.         gets(inputt.firstname);
  56.  
  57.         printf("\nEnter phone number (XXX-XXX-XXXX):  ");
  58.         gets(inputt.phonenum);
  59.  
  60.         fwrite(&inputt, sizeof(inputt), 1, fptr);
  61.  
  62.         /*  Close the data file.  */
  63.  
  64.         fclose(fptr);
  65.  
  66.         exit(0);
  67.  
  68.     }
  69.  
  70.  
  71.     /*  Open data file for reading.  */
  72.  
  73.     if((fptr=fopen("input","rb")) == NULL)
  74.     {
  75.         puts("\nCannot open file for reading data.");
  76.         exit(1);
  77.     }
  78.  
  79.     /*  Search for a lastname in the file, which matches the arguement.  If
  80.         there is a match, print the name.  Keep count of the number of times
  81.         through the loop.  */
  82.  
  83.     do
  84.     {
  85.         fread(&inputt,sizeof(inputt),1,fptr);
  86.         if(strcmpi(inputt.lastname, argv[1]) == 0)
  87.         {
  88.             printf("\n%s %s:  %s", inputt.firstname, inputt.lastname, inputt.phonenum);
  89.             names++;
  90.         }
  91.         count++;
  92.     }while(count < 10);  /*  For this example, a limit of ten names.  */
  93.  
  94.     /*  If there was no name, print mesage.  */
  95.  
  96.     if(names == 0)
  97.         puts("\nNo name found.");
  98.  
  99.     /*  Close data file.  */
  100.  
  101.     fclose(fptr);
  102.  
  103. }
  104.  
  105.  
  106.