I successfully wrote this program that outputs a report in the terminal.
Though, when I attempt to print the same report to a disk file, I can't seem to figure out how to preserve the 'while' loop and get all the report out to print to disk file.
I've tried a couple of things like removing the 'header' function and placing that syntax in the 'summary' function along with all the necessary 'fprintf' statements in there. The outcome of this is just one line appearing in disk file output. Here is the transactiondata.txt file:
IMPORT -25.19
EXPORT 35.41
EXPORT 100.25
IMPORT -500.34
EXPORT 240.35
IMPORT -134.56
EXPORT 459.56
How should I structure the code so that I get the same output that appears in the terminal window in a disk file?
The disk file output should look like this
The MoneyMaking Corporation
550 Warm Sands Drive
Palm Springs, CA 92262
Type Amount Net
---- ------ ---
IMPORT -25.19 -25.19
EXPORT 35.41 10.22
EXPORT 100.25 110.47
IMPORT -500.34 -389.97
EXPORT 240.35 -149.52
IMPORT -134.56 -284.08
EXPORT 459.56 175.48
The net total for the month is $174.48
Transactions processed: 7
This is my code:
//C Libraries
#include <stdio.h>
#include <math.h>
//Global Variable Declarations
FILE *datafile; //disk file (for input)
FILE *reportfile; // report file (for output)
char type [7]; //Transaction Type: either IMPORT or EXPORt
float amount; //Transaction Amount
float absamount; //abs amount
float total; //Total
float runningsum; // End net balance at end of every transaction
int count; //Count of transactions
// Function Prototypes
void header (void);
void process (void);
void summary (void);
//*************************************************
//*************************************************
// M A I N F U N C T I O N
//*************************************************
//*************************************************
int main (void){
// Initialize accumulating variables
datafile = fopen("c:\\class\\mod5\\examples\\transactiondata.txt","r"); // Open input file
count = 0;
total = 0.00;
runningsum = 0.00;
// Produce Report
header();
process();
summary();
fclose(datafile);
system("pause");
return 0;
}
// *************************************************************
// Header - prints a header on the report
// *************************************************************
void header (void)
{
printf("The MoneyMaking Corporation\n");
printf("550 Warm Sands Drive\n");
printf("Palm Springs, CA 92262\n\n");
printf("Type Amount Net\n");
printf("---- ------ ---\n");
}
// *************************************************************
// Process - produces the detail lines of the report
// *************************************************************
void process (void)
{
while(!feof(datafile))
{
fgets(type, 7, datafile);
fscanf(datafile,"%f\n", &amount);
absamount = fabs(amount);
runningsum = amount + runningsum;
printf("%s %15.2f %17.2f\n",type,absamount,runningsum);
count++; // You could also use count = count + 1
total = total + amount;
}
}
// *************************************************************
// Summary - prints the report summary (including accumulators)
// *************************************************************
void summary (void)
{
// Local Variables
float net; //net values
printf("\nThe net total for the month is $%1.2f\nTransactions processed: %d\n", total, count);
}