Hello all,
I am trying to extract weather data from a (.dat) file. The contents of this space delimited file look like this:
2534 80 12 22 105 134 0
2971 74 9 20 106 163 0
2971 62 1 11 112 102 0
...
...
My problem is that I don't know the code to loop to the EOF in my while loop. I know it exists just not how to do it in this case.
Here is my code:
Code:
#include <stdio.h>
#define arrSize 33
int main()
{
/* variable declarations */
int stationNumberInput;
int loop1;
int stNum,
year,
month,
day,
hiTemp,
lowTemp,
rain;
int tempArray[arrSize] = {0};
int daysArray[arrSize] = {0};
double avg;
FILE *in;
/* prompt for inuput */
printf("Please enter station number: ");
scanf("%d", &stationNumberInput);
/* attempt to open the file */
in = fopen("weather.dat", "r");
if(in == NULL)
{
printf("Error opening file. Program terminated.\n");
return(1);
}
/* scan the file, store the temp, keep track of number of days */
while( ??? )
{
fscanf(in, "%d %d %d %d %d %d %d", &stNum, &year, &month, &day, &hiTemp, &lowTemp, &rain);
if(stNum == stationNumberInput)
{
tempArray[year-48] += (hiTemp - 100);
daysArray[year-48] += 1;
}
}
/* print results */
printf("\tYear\tRainfall\n");
printf("\t----\t--------\n");
for(loop1=0; loop1<arrSize; ++loop1)
{
avg = tempArray[loop1] / (double)daysArray[loop1];
printf("\t%d\t%f\n", loop1+48, avg);
}
fclose (in);
return 0;
}
Thanks in advance.