Hey guys,
just doing a coursework assignment and need some help.
My code so far:
Code:
#include "stdio.h"
int main(void)
{
FILE *fptr;
if((fptr = fopen("table.txt", "r")) == NULL) //Open file.
{
printf("\n\nCould not open the table file.\n\n"); //error message.
}
char str1[20], k=0;
while(k != '\n')//Skips the first line (column headers)
{
if(fscanf(fptr, "%c", &k) == EOF) //Or breaks anyway if end of file is reached.
{
printf("\n END OF FILE REACHED\n\n");
break;
}
}
float c02[100], total=0.0;
int END = 0;
int j;
int i;
for(j=0; END == 0; j++) //breaks if end of file is reached
{
for(i=0; i<26; i++)
{
if(fscanf(fptr, "%c", &k) == EOF)
END = 1;
}
fscanf(fptr, "%f", &c02[j]);
if(END == 0)
total = total + c02[j];
}
printf("\nTotal C02: %f kg\n", total);
printf("\nCO2 Rations: %f", total / 2500);
getchar(); //PAUSE
return 0;
}
Basically, this looks at a file - 'table.txt' which has amounts of CO2 at 26 spaces from the left. This code calculates the total amount of CO2 (i.e. add up numbers in all lines - 26 spaces from the left). And calculates CO2 rations (1 ration = 2500 kg/n co2).
But i also need to basically copy all information in the file.
This is the file:
Code:
Fuel/Activity Unit kg CO2 / unit
Petrol 1 litre 2.3
Oil 1 litre 2.7
Coal 1 kg 2.4
Wood 1 kg 0.0
Electricity 1 kWhr 0.4
Natural Gas 1 kWhr 0.2
Air travel 1 mile 0.3
I need the program to copy the left column and the right column (fuel/activity, and kg CO2/unit).
Can anyone help?
Thanks.
Noori.