Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- //Function declarations
- int readLine(FILE* spInventory, int *part, int *dept, char *type, float *cost, int *qty);
- float calcValue(float cost, int qty, char type, float* value, float* totA, float* totB);
- void printLine(FILE* spReport, int *part, int *dept, char *type, float *cost, int *qty );
- int main(void)
- {
- // Local Declarations.
- FILE* spInventory;
- FILE* spReport;
- float cost;
- int qty;
- char type;
- float value;
- float totA;
- float totB;
- int part;
- int dept;
- // Statements
- printf("Processing data\n");
- if (!(spInventory = fopen("inventory.txt", "r")))
- {
- printf("\aError opening file\n");
- return 100;
- }
- if (!(spReport = fopen ("report.txt", "w")))
- {
- printf("\aError opening grades file\n");
- return 102;
- } // if open output
- while (readLine(spReport, &part, &dept, &type, &cost, &qty))
- {
- calcValue(cost,qty, type, &value, &totA, &totB);
- //printLine();
- }
- fclose(spInventory);
- fclose(spReport);
- printf("End data processing");
- return 0;
- } //main
- int readLine(FILE* spInventory, int *part, int *dept, char *type, float *cost, int *qty)
- {
- // Local Declarations
- int ioResult;
- ioResult = fscanf(spInventory, "%d %d %c %f %d\n", part, dept, type, cost, qty);
- if (ioResult == EOF) //end of file, return false - no more to read
- return 0;
- else if (ioResult != 4) //error, return false - no more to read
- {
- printf("\aError reading data\n");
- return 0;
- }
- else // if no error and not EOF, return true there's more to read
- {
- return 1;
- }
- } // end of readLine
- float calcValue(float cost, int qty, char type, float* value, float* totA, float* totB)
- {
- *value = cost * qty;
- *totA = *totA + *value;
- *totB = *totB + *value;
- return 0;
- }
- void printLine(FILE* spReport, int *part, int *dept, char *type, float *cost, int *qty )
- {
- fprintf(spReport, "%d %d %c %f %d", part, dept, type, cost, qty);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement