1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
/* Nick Vincent */
/* Homework 2
Reading and printing a sequential file */
#include <stdio.h>
int main()
{
int id; /* account number */
char name[ 10 ]; /* account name */
long balance; /* account balance */
int rate; /* performance rate */
long salary; /* starting salary */
long inc; /* amount of increase */
long newSalary; /* New multiplied salary */
double perf1;
double perf2;
double perf3;
double perf4;
perf1 = .045;
perf2 = .060;
perf3 = .078;
perf4 = .090;
FILE *cfPtr; /* cfPtr = data.in file pointer */
/* fopen opens file; exits program if file cannot be opened */
if ( ( cfPtr = fopen( "data.in", "r" ) ) == NULL ) {
printf( "File could not be opened\n" );
} /* end if */
else { /* read account, name and balance from file */
printf( "%-10s%-15s%-12s%-12s%-12s%-12s\n", "ID", "Name", "PR", "Current", "Incr", "New" );
fscanf_s( cfPtr, "%d%s%lf%d%1f", &id, name, &balance, &rate, &salary);
/* while not end of file */
while ( !feof( cfPtr ) ) {
if ( rate == 1 ) {
inc = ( perf1 * salary );
newSalary = ( salary + inc );
}
if ( rate == 2 ) {
inc = ( perf2 * salary );
newSalary = ( salary + inc );
}
if ( rate == 3 ) {
inc = ( perf3 * salary );
newSalary = ( salary + inc );
}
if ( rate == 4) {
inc = ( perf4 * salary );
newSalary = ( salary + inc );
}
printf( "%-10d%-15s%-12f%-12f%-12f%-12f\n", &id, name, balance, &rate, &salary, &inc, &newSalary );
fscanf_s( cfPtr, "%d%s%lf%d%1f%d%1f", &id, name, &balance, &rate, &salary, &inc, &newSalary );
} /* end while */
fclose( cfPtr ); /* fclose closes the file */
} /* end else */
getchar();
return 0; /* indicates successful termination */
} /* end main */
|