Listing 2

  1: /*
  2:  * MDBFTXT.C
  3:  * Program:     Mini-Database
  4:  * Written by:  Leor Zolman
  5:  * Module:      File I/O, Text Representation Version
  6:  */
  7:
  8: #include <stdio.h>
  9: #include <stdlib.h>
 10: #include "mdb.h"
 11:
 12: /*
 13:  * Function:       read_db
 14:  * Purpose:        Load_an existing Database from disk
 15:  * Parameters:     Name of Database to load
 16:  * Return Value:   NULL on error, else # of records.
 17:  */
 18: int read_db(char *filename)
 19: {
 20:     FILE *fp;           /* File pointer         */
 21:     int rec_no = 0;     /* # of records read    */
 22:     struct record *rp;  /* Single record ptr    */
 23:     int nitems;
 24:     int active;         /* Temporary variables  */
 25:     char last[26], first[20];   /* to hold the  */
 26:     long id;            /* values of fields     */
 27:     int age;            /* during file input    */
 28:     char gender;
 29:     float salary;
 30:
 31:     max_recs = MAX_RECS;
 32:
 33:     if ((fp = fopen(filename, "r")) == NULL)
 34:     {
 35:         printf("Database not found.\n");
 36:         return NULL;
 37:     }
 38:
 39:     while (1)
 40:     {           /* Read one record (one line) of data: */
 41:         nitems = fscanf(fp, "%d %s %s %ld %d %c %f\n",
 42:             &active, last, first, &id, &age,
 43:             &gender, &salary);
 44:         if (nitems == EOF)      /* stop reading on EOF  */
 45:             break;
 46:         if (nitems != 7)        /* Check for bad record */
 47:         {
 48:             printf("Warning: Last record ignored ");
 49:             printf("(matched only %d items!)\n", nitems);
 50:             break;
 51:         }
 52:                     /* Allocate memory for one record: */
 53:         if ((rp = alloc_rec()) == NULL)
 54:         {
 55:             printf("Out of memory loading Database.\n");
 56:             return NULL;
 57:         }
 58:                     /* rp points to the memory area     */
 59:         rp->active = active;    /* assign field values: */
 60:         strcpy(rp->last, last);
 61:         strcpy(rp->first, first);
 62:         rp->id = id;
 63:         rp->age = age;
 64:         rp->gender = gender;
 65:         rp->salary = salary;
 66:                     /* Save pointer to memory area in   */
 67:         RECS[rec_no++] = rp;    /* RECS, and bump count */
 68:     }
 69:
 70:     fclose(fp);     /* Finished reading input file      */
 71:     return rec_no;  /* Return number of records read    */
 72: }
 73:
 74: /*
 75:  * Function:        write_db
 76:  * Purpose:         Write current Database to disk
 77:  * Parameters:      Name of Database
 78:  * Return Value:    None
 79:  */
 80: void write_db(char *filename)
 81: {
 82:    FILE *fp;
 83:    int rec_no, result;
 84:    struct record *rp;
 85:
 86:                    /* Write into temporary file first: */
 87:    char *tempname = "TEMPFILE.$$$";
 88:    if ((fp = fopen(tempname, "w")) == NULL)
 89:    {
 90:        printf("Can't open temporary file %s ", tempname);
 91:        printf("for writing.\n");
 92:        return;
 93:    }
 94:
 95:    printf("Writing Database %s To Disk...\n", filename);
 96:            /* Each loop iteration writes one record:   */
 97:    for (rec_no = 0; rec_no < n_recs; rec_no++)
 98:    {
 99:        rp = RECS[rec_no];      /*  set rp to next rec  */
100:                                /* write rec. in ASCII  */
101:        result = fprintf(fp, "%d %s %s %ld %d %c %f\n",
102:            rp->active, rp->last, rp->first, rp->id,
103:            rp->age, rp->gender, rp->salary);
104:        if (result < 0)         /* Check for error      */
105:            error("Error writing output database.\n");
106:    }
107:
108:    fclose(fp);             /* close temporary file     */
109:    remove(filename);       /* remove old version       */
110:    while (rename(tempname, filename) == -1)
111:    {                       /* if renaming didn't work..*/
112:        printf("Error renaming temp file: %s\n",
113:                            _strerror(NULL));
114:        printf("Please enter a new filename: ");
115:        gets(filename);     /* try for a legal filename */
116:    }
117: printf("Database written successfully.\n");
118: }