Code:
#include <stdafx.h>
int getInventory(FILE* spIN, int* partNum, int* price, int* qty, int*reorder, int* minOrder);
int writeReport (FILE* spOUT,int partNum, int price, int qty, int reorder, int minOrder, int* orderAmt);
void calcAmount (int partNum, int price, int* qty, int reorder, int minOrder, int* orderAmt);
int main(void)
{
FILE* spIN;
FILE* spOUT;
int partNum;
int price;
int qty;
int reorder;
int minOrder;
int orderAmt;
printf("Begin inventory reporting\n");
if (!(spIN = fopen ("inventory.txt", "r")))
{
printf("Error opening inventory file\n");
return 100;
}
if (!(spOUT = fopen ("report.txt", "w")))
{
printf("Error opening report file\n");
return 102;
}
while(getInventory
(spIN, &partNum, &price, &qty, &reorder, &minOrder))
{
calcAmount (partNum, price, &qty, reorder, minOrder, &orderAmt);
writeReport (spOUT, partNum, price, qty, reorder, minOrder, orderAmt);
}
fclose (spIN);
fclose (spOUT);
printf("End of inventory reporting\n");
return 0;
}
int getInventory (FILE* spIN, int* partNum, int* price, int* qty, int* reorder, int* minOrder)
{
int ioResult;
ioResult = fscanf(spIN, "%4d%4d%2d%2d%2d", partNum, price, qty, reorder, minOrder);
if (ioResult == EOF)
return 0;
else if (ioResult != 5)
{
printf("Error reading data\n");
return 0;
}
else
return 1;
}
void calcAmount (int partNum, int price, int* qty, int reorder, int minOrder, int* orderAmt)
{
*orderAmt = ((reorder + minOrder) - *qty);
if (*qty < reorder)
*orderAmt = *orderAmt;
else
*orderAmt = 0;
return;
}
int writeReport (FILE* spOUT, int partNum, int price, int qty, int reorder, int minOrder, int orderAmt)
{
fprintf(spOUT," %d %d %d %d %d %d\n", partNum, price, qty, reorder, minOrder, orderAmt);
return 0;
}
INPUT FILE
0123 01.23 23 20 20
0234 02.34 34 50 25
3456 34.56 56 50 10
4567 45.67 07 10 05
5678 06.78 75 75 25
OUTPUT FILE
0123 1.230000 23 20 20
0234 2.340000 41 50 25
3456 34.560001 56 50 10
4567 45.669998 8 10 5
5678 6.780000 75 75 25