{"id":5690,"date":"2023-01-01T00:01:42","date_gmt":"2023-01-01T08:01:42","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5690"},"modified":"2024-03-27T16:16:00","modified_gmt":"2024-03-27T23:16:00","slug":"how-big-is-that-file","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5690","title":{"rendered":"How Big is That File?"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9734; &#9734;<\/h2>\n<p>The <em>stat()<\/em> function returns various tidbits about a file, including its timestamp, permissions, file type, and the file&#8217;s size in bytes. This value can also be obtained without without using the <em>stat()<\/em> function, which is this month&#8217;s Exercise.<br \/>\n<!--more--><br \/>\nI suppose I could stop writing this post with that last paragraph, wish you good luck and make this the shortest Exercise post in the blog&#8217;s history. But I&#8217;m willing to offer more support.<\/p>\n<p>Here is code that uses the <em>stat()<\/em> function to pull in a file&#8217;s size. It&#8217;s only loosely related to this Exercise&#8217;s solution.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_01_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_01_01-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;sys\/stat.h&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    char *filename;\r\n    struct stat fs;\r\n    int r;\r\n    \r\n    <span class=\"comments\">\/* check for filename argument *\/<\/span>\r\n    if( argc&lt;2 )\r\n    {\r\n        puts(\"Specify a filename\");\r\n        return(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* operate from the first argument *\/<\/span>\r\n    filename = argv[1];\r\n\r\n    r = stat(filename,&amp;fs);\r\n    if( r==-1 )\r\n        printf(\"Unable to read %s\\n\",filename);\r\n    else\r\n        printf(\"%s is %lld bytes long\\n\",filename,fs.st_size);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The program checks for a command line argument and fails with an error message if one is missing.<\/p>\n<p>When an argument is present, the <em>stat()<\/em> function gathers details about the file. If the <em>stat()<\/em> function returns successfully (the return value <code>r<\/code> isn&#8217;t -1), the file&#8217;s size in bytes is output.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>$ .\/a.out sonnet18.txt<br \/>\nsonnet18.txt is 636 bytes long<\/code><\/p>\n<p>You can read more about the <em>stat()<\/em> function <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3263\">here<\/a>. But this challenge is about <em>not<\/em> using the <em>stat()<\/em> function. Instead, use some other file manipulation\/examination function to determine the file&#8217;s length.<\/p>\n<p>As a major hint, it&#8217;s possible to use random file access tools to gather how many bytes are in a file.<\/p>\n<p>Ensure that the program reports how many bytes are in the named file, looking like the output shown above. You can view <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5699\">my solution<\/a>, but only after you try this exercise on your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C library has a function that reads a file&#8217;s size, but can you obtain the file size without using it? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5690\">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-5690","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\/5690","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=5690"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5690\/revisions"}],"predecessor-version":[{"id":6333,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5690\/revisions\/6333"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}