{"id":3631,"date":"2019-06-15T00:01:15","date_gmt":"2019-06-15T07:01:15","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3631"},"modified":"2019-06-08T10:37:20","modified_gmt":"2019-06-08T17:37:20","slug":"to-read-a-chunk-a-data","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3631","title":{"rendered":"To Read a Chunk a Data"},"content":{"rendered":"<p>Say you must read 2048 bytes from a file. Which of the following <em>fread()<\/em> statements works for you: A, B, both, or neither?<\/p>\n<p>A. <code>c = fread( buffer, 2048, 1, fh);<\/code><\/p>\n<p>B. <code>c = fread( buffer, 1, 2048, fh);<\/code><br \/>\n<!--more--><br \/>\nThe difference between these statements is the order of the second and third arguments. As a review, the second argument is the size of data chunk to read and the third is the number of chunks. To read 2048 bytes, it seems like either choice works.<\/p>\n<p>For option A, the chunk size is 2048 bytes and one chunk is read. That means 2048 bytes are read, right?<\/p>\n<p>For option B, the chunk size is one byte and the number of chunks to read is 2048. Again, the total is 2048. So what&#8217;s the difference?<\/p>\n<p>The difference is that option B performs as expected. Option A reads only one byte. Here&#8217;s sample code for option A:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char filename[] = \"declaration.txt\";\r\n    char buffer[2048];\r\n    FILE *fh;\r\n    int r;\r\n\r\n    <span class=\"comments\">\/* open the file *\/<\/span>\r\n    fh = fopen(filename,\"r\");\r\n    if(!fh)\r\n    {\r\n        fprintf(stderr,\"Unable to open %s\\n\",filename);\r\n        return(1);\r\n    }\r\n\r\n    r = fread( buffer, 2048, 1, fh );\r\n    printf(\"%d bytes read\\n\",r);\r\n\r\n    fclose(fh);\r\n    return(0);\r\n}<\/pre>\n<p>This code opens the file <code><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/10\/declaration.txt\">declaration.txt<\/a><\/code> for reading at Line 11. The <em>fread()<\/em> function at Line 18 seems like it would pull 2048 characters (one 2048-byte chunk) from the file, a value returned in variable <code>r<\/code>. But when the code runs, you see this output:<\/p>\n<p><code>1 bytes read<\/code><\/p>\n<p>Given the description from the <em>fread()<\/em> function&#8217;s man page, you would think the statement would read 2048 bytes: The <em>size<\/em> argument is 2048 and the <em>nitems<\/em> argument is 1. Even if you cast the value 2048 as a <em>size_t<\/em> data type, only one byte is read.<\/p>\n<p>When the arguments are swapped, however, the program runs as expected:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char filename[] = \"declaration.txt\";\r\n    char buffer[2048];\r\n    FILE *fh;\r\n    int r;\r\n\r\n    <span class=\"comments\">\/* open the file *\/<\/span>\r\n    fh = fopen(filename,\"r\");\r\n    if(!fh)\r\n    {\r\n        fprintf(stderr,\"Unable to open %s\\n\",filename);\r\n        return(1);\r\n    }\r\n\r\n    r = fread( buffer, 1, 2048, fh );\r\n    printf(\"%d bytes read\\n\",r);\r\n\r\n    fclose(fh);\r\n    return(0);\r\n}<\/pre>\n<p>The only change made above is to Line 18: arguments 1 and 2048 are swapped. Here&#8217;s the output:<\/p>\n<p><code>2048 bytes read<\/code><\/p>\n<p>Of course, the proper way to write the statement is:<\/p>\n<p><code>r = fread( buffer, sizeof(char) , 2048, fh );<\/code><\/p>\n<p>The <code>declaration.txt<\/code> file is text, so the data chunk being read is a character; the data-chunk size is one character.<\/p>\n<p>As a confession, the reason I wrote this post is because this exact puzzle recently tripped me up. I couldn&#8217;t understand why the <em>fread()<\/em> function was gobbling up only one byte from a file. Yet, when I swapped the second and third arguments, the code ran as expected. Weird, but important.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>fread()<\/em> function can confuse you when you get the arguments wrong. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3631\">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-3631","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\/3631","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=3631"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3631\/revisions"}],"predecessor-version":[{"id":3643,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3631\/revisions\/3643"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}