0

The data file has weather data for Miami and Toronto (city name, the date, temperature, precipitation). I need to create a function that prints a struct but the values are in a .txt file. This is the file: (info.txt)

Miami,2,-6.4,0
Toronto,2,9.5,0.8
Miami,3,-11.7,0
Toronto,3,6.1,0

This is the struct:

struct Weather{

    char location;  //will be "M" or "T"
    int daynum;
    double temp;
    double precip;
};

This is a parser function:

void parseLine(char toParse[], struct Weather * toLoad)
{     
    char * theToken;     
    theToken = strtok(toParse, ",");     
    toLoad->location = theToken[0];     
    theToken = strtok(NULL, ",");     
    toLoad->daynum = atoi(theToken);     
    theToken = strtok(NULL, ",");     
    if(theToken != NULL)
    {         
        toLoad->temp = atof(theToken);     
    }     
    else     
    {         
        toLoad->temp = -400;     
    }     
    theToken = strtok(NULL, ",");     
    if(theToken != NULL)     
    {         
        toLoad->precip = atof(theToken);     
    }     
    else     
    {         
        toLoad->precip = -1.0; 
    }
}

So my question is how do I use the struct and the parse function to create another function that prints the values of location, daynum, temp, and precip.

Note: im using argc and argv in my main function. You should also know im new to file manipulation and so I'm not exactly sure how to use file functions correctly.

Edit: My primary problem is figuring out what to do in 'main' so that I can create the print function

John Smith
  • 129
  • 1
  • 3
  • 12
  • I don't really understand the question. Why can't you just use printf? It seems like you got the "harder" parts done. – SenselessCoder Nov 30 '16 at 20:40
  • What is wrong with using something like printf("%s,%d,%lf,%lf", st->location == 'M' ? "Miami" : "Toronto", st->daynum, st->temp, st->precip); ? – Fernando Coelho Nov 30 '16 at 20:55
  • but don't i have to open the file and do things with the file thats the main problem (in 'main' im not sure what to do) – John Smith Nov 30 '16 at 20:57
  • @JohnSmith Is your intent perhaps to modify the input file? Do you want to read the file, modify it's data, then write it back? In that case you might want to let your `parseLine` function take a `FILE *`, and pass it `stdin`. This way you could run the program like: `myProg.exe < info.txt > info.txt`. – yyny Nov 30 '16 at 21:21
  • no i do not want to modify its data. I will get back to you if your answer works – John Smith Nov 30 '16 at 21:27

1 Answers1

0

Do you want something like this?

void printStruct(FILE *fout, struct Weather *in)
{
    fprintf(fout, "%s,%d,%f,%f", in->location == 'M' ? "Miami" : "Toronto", in->daynum, in->temp, in->precip);
}

Use it as follows:

#include <stdio.h>

#define ELEMSIZE(arr) (sizeof(*arr))
#define ARRAYSIZE(arr) (sizeof(arr)/ELEMSIZE(arr))

int main(void)
{
    char line[1024];
    struct Weather weather;
    FILE *fp;

    fp = fopen("info.txt", "r");
    fread(line, ELEMSIZE(line), ARRAYSIZE(line), fp);
    parseLine(line, &weather);
    printStruct(stdout, &weather);
    fclose(fp);

    return 0;
}

Note: You might want to change the first argument to parseLine to be a FILE * (You will have to do some magic to get strtok to work, but it will improve efficiency as well as flexability and scalability).

yyny
  • 1,623
  • 18
  • 20