{"id":2355,"date":"2017-02-08T00:01:20","date_gmt":"2017-02-08T08:01:20","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2355"},"modified":"2017-03-01T07:52:52","modified_gmt":"2017-03-01T15:52:52","slug":"the-csv-file-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2355","title":{"rendered":"The CSV File &#8211; Solution"},"content":{"rendered":"<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2315\">This month&#8217;s Exercise<\/a>, required you to read a CSV file, extract specific information, and output a table. It&#8217;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.<br \/>\n<!--more--><br \/>\nIn my solution, I use <em>fopen()<\/em> to open the file, then <em>fgets()<\/em> to read in each line from the file. That&#8217;s the easy part. The difficult part is to extract the comma-separated fields. To meet that end, I use the <em>strtok()<\/em> function.<\/p>\n<p>I wrote about the <em>strtok()<\/em> function in a <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1758\">Lesson from February 2016<\/a>. 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 <em>strtok()<\/em> functions to yank out the expected fields from each line of text in the file:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* get year *\/<\/span>\r\nfield=strtok(buffer,\",\");\r\nyear=atoi(field);\r\n<span class=\"comments\">\/* get month *\/<\/span>\r\nfield=strtok(NULL,\",\");\r\nmonth=atoi(field);\r\nmonth--;    <span class=\"comments\">\/* for the months[] array *\/<\/span>\r\n<span class=\"comments\">\/* get day *\/<\/span>\r\nfield=strtok(NULL,\",\");\r\nday=atoi(field);\r\n<span class=\"comments\">\/* get high *\/<\/span>\r\nfield=strtok(NULL,\",\");\r\nhigh=atof(field);\r\n<span class=\"comments\">\/* get low *\/<\/span>\r\nfield=strtok(NULL,\",\");\r\nlow=atof(field);<\/pre>\n<p>The first <em>strtok()<\/em> function reads from input <code>buffer<\/code> up to the first comma. That value is referenced by the <em>char<\/em> pointer <code>field<\/code>. The <em>atoi()<\/em> function converts the string to an integer value.<\/p>\n<p>Subsequent calls to <em>strtok()<\/em> use the <code>NULL<\/code> placeholder to reference the same string. Each value is pulled in turn and converted to an <em>int<\/em> or <em>float<\/em> variable.<\/p>\n<p>After the line is read, a <em>printf()<\/em> statement generates output:<\/p>\n<pre class=\"screen\">\r\nprintf(\"%10s %2d %d:\\tHigh %.1f\\tLow %.1f\\n\",\r\n    months[month],\r\n    day,\r\n    year,\r\n    high,\r\n    low);<\/pre>\n<p><code>months[]<\/code> is an array of strings. The <code>month<\/code> variable is decremented after it&#8217;s read, so that January is month 0 and not month 1.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/02exercise.c\">Click here<\/a> 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&#8217;re good:<\/p>\n<pre><code>  December 25 2016:\tHigh 29.0\tLow 19.0\r\n  December 26 2016:\tHigh 29.0\tLow 20.0\r\n  December 27 2016:\tHigh 36.0\tLow 27.0\r\n  December 28 2016:\tHigh 32.0\tLow 12.0\r\n  December 29 2016:\tHigh 37.0\tLow 20.0\r\n  December 30 2016:\tHigh 34.0\tLow 12.0\r\n  December 31 2016:\tHigh 33.0\tLow 6.0\r\n   January  1 2017:\tHigh 27.0\tLow 18.0\r\n   January  2 2017:\tHigh 20.0\tLow 10.0\r\n   January  3 2017:\tHigh 16.0\tLow 7.0\r\n   January  4 2017:\tHigh 16.0\tLow 1.0\r\n   January  5 2017:\tHigh 18.0\tLow -2.0\r\n   January  6 2017:\tHigh 24.0\tLow 0.0\r\n   January  7 2017:\tHigh 20.0\tLow 4.0\r\n   January  8 2017:\tHigh 25.0\tLow 18.0\r\n   January  9 2017:\tHigh 34.0\tLow 24.0\r\n   January 10 2017:\tHigh 32.0\tLow 16.0\r\n   January 11 2017:\tHigh 21.0\tLow 5.0\r\n   January 12 2017:\tHigh 26.0\tLow -3.0\r\n   January 13 2017:\tHigh 24.0\tLow 3.0\r\n   January 14 2017:\tHigh 27.0\tLow 9.0\r\n   January 15 2017:\tHigh 18.0\tLow -1.0\r\n   January 16 2017:\tHigh 25.0\tLow 8.0\r\n   January 17 2017:\tHigh 30.0\tLow 10.0\r\n   January 18 2017:\tHigh 40.0\tLow 28.0\r\n   January 19 2017:\tHigh 40.0\tLow 28.0\r\n   January 20 2017:\tHigh 41.0\tLow 30.0\r\n   January 21 2017:\tHigh 37.0\tLow 30.0\r\n   January 22 2017:\tHigh 38.0\tLow 32.0\r\n   January 23 2017:\tHigh 39.0\tLow 28.0\r\n   January 24 2017:\tHigh 36.0\tLow 30.0<\/code><\/pre>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2384\">March&#8217;s Exercise<\/a>, you&#8217;ll read and manipulate the data, which is a far more common activity.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise, required you to read a CSV file, extract specific information, and output a table. It&#8217;s basically a file-reading exercise, though you must also translate the input into the proper value. And you must output the month as &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2355\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2355","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2355"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2355\/revisions"}],"predecessor-version":[{"id":2407,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2355\/revisions\/2407"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}