Final Homework

Due: Monday, December 14, 1998

Please note: the final exam will be held on:

Date: Wed 12/16/98 8-9:50am <--- NOTE THE TIME

Location: Main/703 <--- NOTE THE LOCATION

The final homework requires you to write only one function. The rest of the program is already done for you.


The program below reads and writes a database of employee records. Note that each employee record is really a structure consisting of two elements: firstName and lastName. The main function stores all employee records in an array:
struct employee company[MAXEMPLOYEES];
This creates an array of 10 employee structures.

What's already done for you

Most of the program is already done for you. Specifically, the readDatabase() function is already complete. Functionality for viewing all records and adding a new record is also complete.

What you need to add

You need to write the writeDatabase() function. The function prototype is as follows:
void writeDatabase (struct employee *compPtr, int records)

The first parameter is a pointer to an employee structure. The second parameter represents the number of records in the database. Your function must write these records to the employee.txt file.

Each database record consists of one line: firstName[space]lastName.

Here is the partially complete program:

/*  Final Homework                                                */
/*  This program reads and writes a simple Employee Database file */
/*  Your job is to write the writeDatabase() Function             */
/*  Ethan Cerami, NYU                                             */
/*  December 6, 1998                                              */

#include <stdio.h>
#define MAXCHARS 30

struct employee {
   char firstName[30];
   char lastName[30];

void printOptions (void);
int readDatabase (struct employee *);
void writeDatabase (struct employee *, int);

main () {
   struct employee company[MAXEMPLOYEES];
   int records, option, i;
   printf ("Welcome to the Globix Employee Database.\n\n");

   records = readDatabase(company);
   printf ("Reading in %d records.\n\n", records);
   printf ("\nPlease select an option:  ");
   scanf ("%d", &option);
   while (option != 4) {
   switch (option) {
        /*  Display Employee Records */
        case 1:
            printf ("\nEmployee Records:\n");
            printf ("-----------------\n");
            for (i=0; i< records; i++)
                printf ("%s\t%s\n", company[i].firstName, company[i].lastName);
            printf ("\n");
         /*  Add new Employee to Company array */
         /*  Note the use of the dot operator */
         case 2:
            if (records<MAXEMPLOYEES) {
                printf ("\nEnter first Name:  ");
                scanf ("%s", company[records].firstName);
                printf ("Enter last Name:  ");
                scanf ("%s", company[records].lastName);
            else printf ("Database if full.\n");
            printf ("\n");
         case 3:
            writeDatabase(company, records);
            printf ("Invalid option.\n");
      printf ("Please select an option:  ");
      scanf ("%d", &option);

/*  Display Menu Options */
void printOptions (void) {
   printf ("1.  View all Employee Records\n");
   printf ("2.  Add New Employee\n");
   printf ("3.  Save revised Employee List\n");
   printf ("4.  Exit\n");

/*  Open Employee.txt file and pass Pointer to company array    */
/*  At first, compPtr points to &company[0]                         */
int readDatabase (struct employee *compPtr) {
   int counter=0;
   FILE *fPtr;

   /*  Open file for reading.  Check for errors */
   if ((fPtr = fopen ("employee.txt", "r"))==NULL)
       printf ("File could not be opened.\n");
   else {
      /*  Prime the while loop with initial data */
      fscanf (fPtr, "%s", compPtr->firstName);
      fscanf (fPtr, "%s",  compPtr->lastName);

      /*  Keep looping until END OF FILE (EOF) is reached */
      while (!feof (fPtr)) {
         /*  Increment Company Pointer and record counter*/
         /*  compPtr now points to next element in the array */

         /*  Read in new data */
         /*  Note the user of the arrow operator */
         fscanf (fPtr, "%s", compPtr->firstName);
         fscanf (fPtr, "%s", compPtr->lastName);

   /*  Always make sure to close the file */
   fclose (fPtr);

   /*  Return number of records read */
   return counter;

void writeDatabase (struct employee *compPtr, int records) {
   /*  This is the function you need to write */