The input file salesintxt contains a series of lines with th
The input file salesin.txt contains a series of lines with three numbers on each line. Each line corresponds to a certain item which has been bought and then subsequently sold. The first number is the buying price of the item, the second number is the selling price of the item, and the third number is the quantity sold (integer). We are interested in keeping track of the total profit and total loss over all of the items listed in the file. For example, if a line in the file contains 1.34 1.87 4, then that item yielded a profit of 4(1.87 1.34) = $2.12 and this number should be added to the total profit. Similarly, if another line reads 3.22 2.87 6, then that item had a loss of 6(3.22 2.87) = $2.10 and this number should be added to the total loss.
Write a program, sales.c, which repeatedly reads the buying, selling, and quantity values from salesin.txt and reports the information to an output file salesout.txt along with the item profit and total running profit, or the item loss and total running loss.
// salesin.txt contains the following lines:
/// then salesout.txt should contain:
2.56 3.44 8: profit = 7.04, total profit = 7.04
1.24 1.69 5: profit = 2.25, total profit = 9.29
8.44 7.32 3: loss = 3.36, total loss = 3.36
3.67 4.12 8: profit = 3.60, total profit = 12.89
4.33 4.01 5: loss = 1.60, total loss = 4.96
Solution
C Program Code
#include <stdio.h> // Incluinf the header files here
int main()
{
FILE * ptrin, *ptrout; // function pointers
int c;
float b,s,profit,loss,TotPro=0.0,TotLos=0.0;
ptrin = fopen(\"salesin.txt\",\"r\"); // opening the input file salesin.txt
ptrout = fopen(\"salesout.txt\",\"w\"); // opening the output file salesout.txt
do // loop to access the element of file salesin.txt on eby one
{
fscanf(ptrin,\"%f%f%d\ \",&b,&s,&c); // reading the salesin.txt file line by line
if((s-b)>=0) // checking profit or loss
{ // if profit
profit = c*(s-b); // calculating profit
TotPro = TotPro + profit; // adding profit to the total profit
// printing the result in the file salesout.txt
fprintf(ptrout,\"%5.2f %5.2f %d: profit = %5.2f, total profit = %5.2f\ \",b,s,c,profit,TotPro);
}
else // in case of loss
{
loss = c*(b-s); // calculating loss
TotLos = TotLos + loss; // adding loss to the total loss
// saving the results in the file salesout.txt
fprintf(ptrout,\"%5.2f %5.2f %d: loss = %5.2f, total loss = %5.2f\ \",b,s,c,loss,TotLos);
}
}while(!feof(ptrin));// exit the loop while end of file reache
return 0;
} // end of program
The salesin.txt file
2.56 3.44 8
1.24 1.69 5
8.44 7.32 3
3.67 4.12 8
4.33 4.01 5
The salesout.txt file After execution of the program
2.56 3.44 8:profit = 7.04, total profit = 7.04
1.24 1.69 5:profit = 2.25, total profit = 9.29
8.44 7.32 3: loss = 3.36, total loss = 3.36
3.67 4.12 8:profit = 3.60, total profit = 12.89
4.33 4.01 5: loss = 1.60, total loss = 4.96

