{"id":2294,"date":"2017-01-08T00:01:03","date_gmt":"2017-01-08T08:01:03","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2294"},"modified":"2017-01-07T10:07:12","modified_gmt":"2017-01-07T18:07:12","slug":"find-the-error-pointers-structures-and-files-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2294","title":{"rendered":"Find the Error: Pointers, Structures, and Files &#8211; Solution"},"content":{"rendered":"<p>It took me a while to figure out what was wrong with <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/01exercise.c\">this month&#8217;s Exercise file<\/a>. It became an obsession! Of course, I was working with a more complex version &mdash; the original code. In that code, the information is written several times to the file, and it becomes very obvious that something is wrong. But when the data is written only once, it&#8217;s tough to know whether a problem exists.<br \/>\n<!--more--><br \/>\nWhen you examine the code, you read it line-by-line, looking for common errors: stray semicolons, infinite loops, uninitialized pointers. Then you examine the various functions to ensure that they&#8217;re called properly and that all the arguments are in the right order.<\/p>\n<p>In this specific code, the problem is that the data is written incorrectly to the file. Here&#8217;s the original <em>fwrite()<\/em> statement:<\/p>\n<p><code>fwrite(&output, sizeof(output), 1, datfile);<\/code><\/p>\n<p>The function&#8217;s arguments are:<\/p>\n<ol>\n<li>The location (memory address) of the data to write: <code>&amp;output<\/code><\/li>\n<li>The size of the data chunk: <code>sizeof(output)<\/code><\/li>\n<li>The number of chunks to write: <code>1<\/code><\/li>\n<li>The open file handle: <code>datfile<\/code><\/li>\n<\/ol>\n<p>It all looks good, and it did to me, until I noticed that variable <code>output<\/code> is a pointer already. So you don&#8217;t need the ampersand.<\/p>\n<p>Second, the <em>sizeof<\/em> operator is measuring the wrong value. Yes, the program writes a structure&#8217;s data, and that structure is referenced as a pointer, but the structure itself is what&#8217;s written to the file, not the pointer. For the <em>fwrite()<\/em> function, you specify the structure&#8217;s size, not the pointer&#8217;s size. The proper argument is <code>sizeof(struct data)<\/code>, where <code>data<\/code> is the structure&#8217;s name.<\/p>\n<p>The proper <em>fwrite()<\/em> statement should be:<\/p>\n<p><code>fwrite(output, sizeof(struct data), 1, datfile);<\/code><\/p>\n<p>The <em>fwrite()<\/em> function takes the information referenced by pointer <code>output<\/code>, writes the size of the structure <code>data<\/code>, one chunk, to the open file referenced by <code>datfile<\/code>.<\/p>\n<p>Similarly, the <em>fread()<\/em> function later in the code must be repaired. It makes the same errors:<\/p>\n<p><code>fread(&input, sizeof(input), 1, datfile);<\/code><\/p>\n<p>The proper statement is:<\/p>\n<p><code>fread(input, sizeof(struct data), 1, datfile);<\/code><\/p>\n<p>Make these changes and the code runs as intended.<\/p>\n<p>Structure pointers can get messy! The member operators change, but aside from that, you&#8217;re still dealing with structures in memory. That confusion is enough to improperly code a solution and end up with a frustrating programming puzzle.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It took me a while to figure out what was wrong with this month&#8217;s Exercise file. It became an obsession! Of course, I was working with a more complex version &mdash; the original code. In that code, the information is &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2294\">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-2294","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\/2294","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=2294"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2294\/revisions"}],"predecessor-version":[{"id":2313,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2294\/revisions\/2313"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}