The CSV File – Solution

This month’s Exercise, required you to read a CSV file, extract specific information, and output a table. It’s basically a file-reading exercise, though you must also translate the input into the proper value. And you must output the month as a string.

In my solution, I use fopen() to open the file, then fgets() to read in each line from the file. That’s the easy part. The difficult part is to extract the comma-separated fields. To meet that end, I use the strtok() function.

I wrote about the strtok() function in a Lesson from February 2016. What it does is to repeatedly pull chunks of text from a string based on one or more characters, or tokens. In my solution, I employed a series of strtok() functions to yank out the expected fields from each line of text in the file:

/* get year */
field=strtok(buffer,",");
year=atoi(field);
/* get month */
field=strtok(NULL,",");
month=atoi(field);
month--;    /* for the months[] array */
/* get day */
field=strtok(NULL,",");
day=atoi(field);
/* get high */
field=strtok(NULL,",");
high=atof(field);
/* get low */
field=strtok(NULL,",");
low=atof(field);

The first strtok() function reads from input buffer up to the first comma. That value is referenced by the char pointer field. The atoi() function converts the string to an integer value.

Subsequent calls to strtok() use the NULL placeholder to reference the same string. Each value is pulled in turn and converted to an int or float variable.

After the line is read, a printf() statement generates output:

printf("%10s %2d %d:\tHigh %.1f\tLow %.1f\n",
    months[month],
    day,
    year,
    high,
    low);

months[] is an array of strings. The month variable is decremented after it’s read, so that January is month 0 and not month 1.

Click here to view my full solution. As usual, your solution may vary, but as long as the output is a table similar to this one, you’re good:

  December 25 2016:	High 29.0	Low 19.0
  December 26 2016:	High 29.0	Low 20.0
  December 27 2016:	High 36.0	Low 27.0
  December 28 2016:	High 32.0	Low 12.0
  December 29 2016:	High 37.0	Low 20.0
  December 30 2016:	High 34.0	Low 12.0
  December 31 2016:	High 33.0	Low 6.0
   January  1 2017:	High 27.0	Low 18.0
   January  2 2017:	High 20.0	Low 10.0
   January  3 2017:	High 16.0	Low 7.0
   January  4 2017:	High 16.0	Low 1.0
   January  5 2017:	High 18.0	Low -2.0
   January  6 2017:	High 24.0	Low 0.0
   January  7 2017:	High 20.0	Low 4.0
   January  8 2017:	High 25.0	Low 18.0
   January  9 2017:	High 34.0	Low 24.0
   January 10 2017:	High 32.0	Low 16.0
   January 11 2017:	High 21.0	Low 5.0
   January 12 2017:	High 26.0	Low -3.0
   January 13 2017:	High 24.0	Low 3.0
   January 14 2017:	High 27.0	Low 9.0
   January 15 2017:	High 18.0	Low -1.0
   January 16 2017:	High 25.0	Low 8.0
   January 17 2017:	High 30.0	Low 10.0
   January 18 2017:	High 40.0	Low 28.0
   January 19 2017:	High 40.0	Low 28.0
   January 20 2017:	High 41.0	Low 30.0
   January 21 2017:	High 37.0	Low 30.0
   January 22 2017:	High 38.0	Low 32.0
   January 23 2017:	High 39.0	Low 28.0
   January 24 2017:	High 36.0	Low 30.0

In March’s Exercise, you’ll read and manipulate the data, which is a far more common activity.

Leave a Reply