{"id":14,"date":"2013-04-20T00:01:34","date_gmt":"2013-04-20T08:01:34","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=14"},"modified":"2013-05-04T18:21:34","modified_gmt":"2013-05-05T02:21:34","slug":"whats-the-sizeof-that","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=14","title":{"rendered":"What&#8217;s the sizeof That?"},"content":{"rendered":"<p>When writing a structure to a file, you need to ensure that you specify the proper structure size. The <code>sizeof<\/code> keyword is obviously the way you determine the size, but what exactly are you getting the size of?<br \/>\n<!--more--><br \/>\nFor example, suppose you have a structure called <code>record<\/code>. Then you have an array of the structure <code>record<\/code> called <code>database<\/code>. You write an individual structure to disk by using the <code>fwrite()<\/code> statement:<\/p>\n<p><code>fwrite(&database[3],sizeof(struct record),1,fptr);<\/code><\/p>\n<p><code>&database[3]<\/code> is the item that you&#8217;re writing to disk, presumably to the file represented by <code>fptr<\/code>. But the size of the item you&#8217;re writing to disk is based on the size of the structure, not the size of that one element. For example, the following statement is incorrect:<\/p>\n<p><code>fwrite(&database[3],sizeof(database[3]),1,fptr);<\/code><\/p>\n<p>Or even this:<\/p>\n<p><code>fwrite(&database[3],sizeof(struct database[3]),1,fptr);<\/code><\/p>\n<p>In either case the <code>sizeof<\/code> operator returns the size of the element you&#8217;re writing, not of the full structure. The two values could be different! Therefore, it&#8217;s best to always use the defined structure size when writing elements to a file.<\/p>\n<p>A great tool to discover how information is written to disk is a file dumper. In Unix, you can use the <em>hexdump<\/em> utility to view raw bytes in a file. For writing a structure to a random access file, you should see the records evenly spread out within the file. Each record starts at a given offset. When you see that offset shift, then you know that the program is not writing data to the file in even chunks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When writing a structure to a file, you need to ensure that you specify the proper structure size in the <em>fwrite()<\/em> function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=14\">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":[2],"tags":[],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/14","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=14"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":44,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/14\/revisions\/44"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}