{"id":2262,"date":"2017-01-01T00:01:03","date_gmt":"2017-01-01T08:01:03","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2262"},"modified":"2017-01-09T08:05:24","modified_gmt":"2017-01-09T16:05:24","slug":"find-the-error-pointers-structures-and-files","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2262","title":{"rendered":"Find the Error: Pointers, Structures, and Files"},"content":{"rendered":"<p>This month&#8217;s Exercise was originally how to create a structure pointer, fill it with data, and write that data to a file. It&#8217;s a procedure fraught with traps and peril. What&#8217;s even more challenging is when you&#8217;re presented with this type of code and it doesn&#8217;t work, but it&#8217;s your job to fix it.<br \/>\n<!--more--><br \/>\nI&#8217;ve spent my share of time debugging someone else&#8217;s work. It&#8217;s maddening! That&#8217;s because you eventually get your own style and preferences, but soon you discover that other programmers lack such discipline.<\/p>\n<p>Even when reviewing my own code, I often get frustrated with myself for not documenting the process in a better way. I could go on . . . but instead, I present this month&#8217;s Exercise, which is based on code a reader asked me to debug.<\/p>\n<p>The code creates a pointer to a structure. Memory is allocated for the structure and the members are assigned values. Then the structure is written to a file, read back from the file, then the contents displayed. The code compiles, which doesn&#8217;t help you debug. Further, it appears as though the program works flawlessly &mdash; but it doesn&#8217;t!<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/01exercise.c\">Click here<\/a> to view the code.<\/p>\n<p>Here is the sample output:<\/p>\n<pre><code>File created!\r\nData saved!\r\nFile opened!\r\nWord: alpha\r\nValue: 1<\/code><\/pre>\n<p>According to the output, the program runs great. Values <code>alpha<\/code> and <code>1<\/code> are written to the file and apparently read back in. But here is the hexdump of the file&#8217;s contents:<\/p>\n<pre class=\"screen\">\r\n00000000  50 27 40 e5 d6 7f 00 00                           |P'@.....|\r\n00000008<\/pre>\n<p>As you can see, the word <code>alpha<\/code> doesn&#8217;t appear in the file. So something has gone awry. Your job is to figure out what.<\/p>\n<p>Please attempt to solve this problem on your own. <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2294\">Click here<\/a> to read how I solved the problem and what&#8217;s specifically wrong with the code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes the programming challenge isn&#8217;t to write the code, it&#8217;s to figure out what someone else has written and why it doesn&#8217;t work. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2262\">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":[3],"tags":[],"class_list":["post-2262","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2262","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=2262"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2262\/revisions"}],"predecessor-version":[{"id":2318,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2262\/revisions\/2318"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}