#include #include #include #define WFILE "temps.txt" typedef struct { char time[10]; char ampm[3]; } Time; void timePrinter(char* target, Time* time) { sprintf(target, "%s %s", time->time, time->ampm); } typedef struct { char direction[10]; int speed; char scale[4]; } Wind; typedef struct { int temperature; char scale[2]; } Temperature; typedef struct { Time time; Temperature temperature; Temperature dewPoint; int relHum; Wind wind; } WeatherData; void wprinter(WeatherData* w) { printf("Time:%s %s Temp:%d %s\n", w->time.time, w->time.ampm, w->temperature.temperature, w->temperature.scale); } void wprinterB(WeatherData* w) { char timee[15]; timePrinter(timee, &w->time); printf("Time:%s Temp:%d %s\n", timee, w->temperature.temperature, w->temperature.scale); } WeatherData* parseB(char* line) { WeatherData* wp = malloc(sizeof(WeatherData)); char* c = strtok(line, " \t"); strcpy(wp->time.time, c); c = strtok(NULL, " \t"); printf("%s\n", c); strcpy(wp->time.ampm, c); c = strtok(NULL, " \t"); wp->temperature.temperature = (int)strtol(c, NULL, 10); c = strtok(NULL, " \t"); strcpy(wp->temperature.scale, c); c = strtok(NULL, " \t"); wp->dewPoint.temperature = (int)strtol(c, NULL, 10); c = strtok(NULL, " \t"); strcpy(wp->dewPoint.scale, c); c = strtok(NULL, " \t"); wp->relHum = (int)strtol(c, NULL, 10); c = strtok(NULL, " \t"); strcpy(wp->wind.direction, c); c = strtok(NULL, " \t"); wp->wind.speed = (int)strtol(c, NULL, 10); c = strtok(NULL, " \t"); strcpy(wp->wind.scale, c); return wp; } int main(void) { WeatherData** weather = malloc(400*sizeof(WeatherData*)); char line[256]; FILE* f = fopen(WFILE, "r"); if (f==NULL) { fprintf(stderr, "Could not open %s -- quitting\n", WFILE); return 1; } int c = 0; while (NULL != fgets(line, 256, f)) { weather[c] = parseB(line); c++; } for (int i=0; i