Hello to all. I am trying to read from a file, weather.txt, into an array (using functions), and print that array once created. I believe I have before and after main done correctly, but the content of main I am lost on. I have never been good at functions, so that's why I am trying this, to learn more.
What I have so far:
Code:
#include <stdio.h>
typedef struct{
int avg_high;
int avg_low;
int avg_wind;
float avg_precip;
} weather_t;
const int nbr_months = 12;
const char weather_file [] = "weather.txt";
FILE * fptr;
int loadstats(FILE * wfile, weather_t wstats[]);
void printstats (weather_t wstats[]);
int main(void)
{
fptr=fopen("wfile", "r");
//I have opened the file, but how do I place it into the array?
return 0;
}
int loadstats(FILE * wfile, weather_t wstats[])
{
int k;
for(k = 0; k < nbr_months; k++)
fscanf(fptr, "%i%i%i%f", &wstats[k]);
printf("%i%i%i%f"), wstats[k];
}
This runs with no error, but does nothing (which makes sense, as there is no code in main). I just don't understand how to read and place into an array. A C reference book I looked at said to use:
Code:
int = fscanf(FILE * , formatstring, list of destination(addresses));
to read from a file into an array. So I think for formatring I would place , because what I wish to read contains three ints and a float, but I am not sure. As well, I don't understand what this piece of code means by destination(addresses), or if it is right at all to use such code. Thank you all for any help you can give!