home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / R_LA4_02.ZIP / AGENTR.C next >
Encoding:
C/C++ Source or Header  |  1988-01-07  |  3.2 KB  |  117 lines

  1. /* AGENTR.C - From page 453 of "Microsoft C Programming for     */
  2. /* the IBM" by Robert Lafore. This is a simple database program.*/
  3. /* I added 2 if statements to file in book. One is after else   */
  4. /* statement in rfile() function. This prevents adding exist-   */
  5. /* ing records to array (a duplication making the array twice   */
  6. /* as large) when selecting 'r' while an array already exists.  */
  7. /* It will then only read a file to array at program startup.   */
  8. /* The second if statement added is in the first case statement.*/
  9. /* This will prevent erasing an existing database by automat-   */
  10. /* ically writing it to disk before selecting enter a name,     */
  11. /* otherwise selecting enter before writing will start the new  */
  12. /* database at n == 0 after opening the disk file.              */
  13. /****************************************************************/
  14.  
  15. #include "stdio.h"
  16. #define TRUE 1
  17.  
  18. struct personnel {                  /*define data structure*/
  19.    char name [40];
  20.    int agnumb;
  21.    float height;
  22. };
  23. struct personnel agent[50];         /*array of 50 structures*/
  24. int n = 0;
  25.  
  26. main()
  27. {
  28. char ch;
  29.  
  30.    while(TRUE) {
  31.       printf("\n'e' Enter new agent\n'l' List all agents");
  32.       printf("\n'w' Write file\n'r' Read file: ");
  33.       ch = getche();
  34.       switch(ch) {
  35.          case 'e':
  36.             if (n == 0)
  37.                rfile();
  38.             newname();
  39.             break;
  40.          case 'l':
  41.             listall();
  42.             break;
  43.          case 'w':
  44.             wfile();
  45.             break;
  46.          case 'r':
  47.             rfile();
  48.             break;
  49.          default:
  50.             puts("\nEnter only selections listed.");
  51.       }
  52.    }
  53. }
  54.  
  55. /* newname */    /* puts a new agent in the database */
  56. newname()
  57. {
  58.  
  59.    printf("\nRecord %d.\nEnter name: ", n + 1);
  60.    gets(agent[n].name);
  61.    printf("\nEnter agent number (3 digits): ");
  62.    scanf("%d", &agent[n].agnumb);
  63.    printf("Enter height in inches: ");
  64.    scanf("%f", &agent[n++].height);
  65.    fflush(stdin);
  66. }
  67.  
  68. /* listall() */   /*lists all agents and data */
  69. listall()
  70. {
  71. int j;
  72.  
  73.    if(n < 1)
  74.       printf("\nEmpty list.\n");
  75.    for(j = 0; j < n; j++) {
  76.       printf("\nRecord number %d\n", j + 1);
  77.       printf("   Name: %s\n", agent[j].name);
  78.       printf("   Agent number: %03d\n", agent[j].agnumb);
  79.       printf("   Height: %4.2f\n", agent[j].height);
  80.    }
  81. }
  82.  
  83. /* wfile */    /* writes array of structures to file */
  84. wfile()
  85. {
  86. FILE *fptr;
  87.  
  88.    if(n < 1) {
  89.       printf("\nCan't write empty list.\n");
  90.       return;
  91.    }
  92.    if((fptr = fopen("c:agents.rec", "wb")) == NULL)
  93.       printf("\nCan't open file agents.rec\n");
  94.    else {
  95.       fwrite(agent, sizeof(agent[0]), n, fptr);
  96.       fclose(fptr);
  97.       printf("\nFile of %d records written.\n", n);
  98.    }
  99. }
  100.  
  101. /* rfile() */  /* reads records from file into array */
  102. rfile()
  103. {
  104. FILE *fptr;
  105.  
  106.    if((fptr = fopen("c:agents.rec", "rb")) == NULL)
  107.       printf("\nCan't open file agents.rec\n");
  108.    else {
  109.       if(n == 0)  /*Fill array only at start, otherwise will dup entire file*/
  110.          while(fread(&agent[n], sizeof(agent[n]), 1, fptr) == 1)
  111.             n++;
  112.    fclose(fptr);
  113.    printf("\nFile read. Total agents is %d.\n", n);
  114.    }
  115. }
  116.  
  117.