Code:
#include <stdio.h>
#include <stdlib.h>
// Function Declarations
int getData (FILE* spDataIn,
int* empNo, int* dept, float* payRate, char* exempt, int* hours);
int writeData (FILE* spDataOut,
int empNo, int dept, float payRate, char exempt, int hours, float basePay);
void calcBasePay (float payRate, int hours, float* basePay);
int main (void)
{
// Local Declarations
FILE* spDataIn;
FILE* spDataOut;
int empNo;
int dept;
float payRate;
char exempt;
int hours;
float basePay;
// Statements
printf("This program creates a payroll register...\n");
if (!(spDataIn = fopen("DATA.txt", "r")))
{
printf("\aError opening employee file\n");
return 100;
} // if open input
if (!(spDataOut = fopen("DATA.txt", "w")))
{
printf("\aError opening employee file\n");
return 102;
} // of open output
while (getData
(spDataIn, &empNo, &dept, &payRate, &exempt, &hours))
{
calcBasePay (payRate, hours, &basePay);
writeData (spDataOut, empNo, dept, payRate, exempt, hours, basePay);
} // while
fclose(spDataIn);
fclose(spDataOut);
printf("End employee payroll register\n");
return 0;
} // main
/*==================getData==================
Reads employee data from text file.
Pre spDataIn is an open file.
empNo, dept, payRate, exempt, hours, basePay pass by address
Post reads employee information
if data read --returns 1
if EOF or error --returns 0
*/
int getData (FILE* spDataIn, int* empNo, int* dept, float* payRate, char* exempt, int* hours)
{
// Local Declarations
int ioResult;
// Statements
ioResult = fscanf(spDataIn, "%d %d %f %c %d", empNo, dept, payRate, exempt, hours);
if(ioResult == EOF)
return 0;
else
return 1;
} // getData
/*==================calcBasePay==================
Determine Base Pay based on hours and payRate.
Pre payRate and hours contain the variables.
Post basePay copied to address.
*/
void calcBasePay (float payRate, int hours, float* basePay)
{
// Statements
*basePay = (payRate*hours);
return;
} // calcBasePay
/*==================writeData==================
Writes employee information, which now includes basePay, to output file.
Pre spDataOut is an open file
empNo, dept, payRate, exempt, hours, basePay have values to write.
Post Data written to file.
*/
int writeData (FILE* spDataOut, int empNo, int dept, float payRate, char exempt, int hours, float basePay)
{
// Statements
fprintf(spDataOut, "%d %d %f %c %d %f\n", empNo, dept, payRate, exempt, hours, basePay);
return 0;
} // writeData
Thank you!