Im a c programming rookie, and i could use some help with writing a data file, reading that file, and manipulating that data.
I guess the best way to help explain this is to make up a simple sample problem.
Lets say your a water billing company with three customers, you charge 10cents per gallon of water if the customer uses 100 gallons or less and 20cents per gallon if the customer goes over 100 gallons.
Customer Numbers-----Gallons Consumed
10--------------------------500
268------------------------20
780------------------------800
First off how would i write this into a datafile (i guess a text file) so i can build a cprogram and read this information?
Like this?
ID1
customer_number 10
gallons_consumed 500
EndID
ID1
customer_number 268
gallons_consumed 20
EndID
ID1
customer_number 780
gallons_consumed 800
EndID
(i save the following information as datafile.txt - im guessing the above is completely incorrect, so i could use assistance on writing a datafile, please)
Next im wondering if this is the correct way to open/read the datafile within my c program:
I dont know how to read or output the inputs from the datafile, the only thing else i think i need is a loop to calculate the cost, but i dont know how i would do this per each customer - something like this?:Code:#include <stdio.h> #include <ctype.h> int main() { int customernumber, gallons_consumed, amountdue; FILE *ifp; ifp = fopen("datafile", "r"); /* opens file for reading */ }
if (gallons_consumed <= 100)
printf gallons_consumed * .10
else
printf gallons_consumed * .20
Then i want to manipulate and output this data into a chart like this:
Customer Number Gallons Consumed Amount Due
10 500 $100.00
268 20 $2.00
70 800 $160.00
Total Customers: 3
Total Gallons Consumed: 1320
Total Amount Due: $262.00
How would i do this, by using a datafile? Since im a rookie, im just used to using scanf - this datafile thing is completely missing with my mind! Any tips, code, or links would be greatly appreciated! Thanks for your time...



LinkBack URL
About LinkBacks



im almost finished i think.