{"id":1735,"date":"2016-01-30T00:01:33","date_gmt":"2016-01-30T08:01:33","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1735"},"modified":"2016-02-06T08:19:06","modified_gmt":"2016-02-06T16:19:06","slug":"the-errno-variable","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1735","title":{"rendered":"The errno Variable"},"content":{"rendered":"<p>One of the C language&#8217;s universal variables is <code>errno<\/code>. It contains a code describing details about why a particular operation failed. You can use <code>errno<\/code> in your code to provide better, more informative error messages for your programs.<br \/>\n<!--more--><br \/>\nThe codes and constants defined by <code>errno<\/code> are listed in the <em>errno.h<\/em> header file. <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/01\/errno.txt\">Click here<\/a> to see an example of how that file may look. Each compiler may list additional code values, but the core values are consistent from machine to machine, especially for file access.<\/p>\n<p>Not all the codes apply to every library function that uses <code>errno<\/code>. For those functions that do, you can find a list of applicable <code>errno<\/code> constants in the function&#8217;s man page reference. The <em>rename()<\/em> function lists a host of <code>errno<\/code> constants, which you can apply to your code to provide a better error message.<\/p>\n<p>The following example updates <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1726\">last week&#8217;s Lesson<\/a> code to provide more detailed error messages:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;errno.h&gt;\r\n\r\nint main()\r\n{\r\n    int result;\r\n\r\n    result = rename(\"this.txt\",\"that.txt\");\r\n    if( result != 0 )\r\n    {\r\n        printf(\"File renaming error: \");\r\n        switch(errno)\r\n        {\r\n            case EPERM:\r\n                printf(\"Operation not permitted\\n\");\r\n                break;\r\n            case ENOENT:\r\n            case ENOFILE:\r\n                printf(\"File not found\\n\");\r\n                break;\r\n            case EACCES:\r\n                printf(\"Permission denied\\n\");\r\n                break;\r\n            case ENAMETOOLONG:\r\n                printf(\"Filename is too long\\n\");\r\n                break;\r\n            default:\r\n                printf(\"Unknown error\\n\");\r\n        }\r\n        return(1);\r\n    }\r\n    puts(\"File renamed\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In this code, the <code>errno.h<\/code> header file is added (Line 2). If the <em>rename()<\/em> function fails, the <em>if<\/em> statement at Line 9 catches it. Line 11 prints a generic error message, but the <em>switch-case<\/em> structure (Lines 12 to 29) scans common <code>errno<\/code> variable values to add more detail.<\/p>\n<p>This code uses defined constants, so I specified both variations on the <code>File not found<\/code> constant, <code>ENOENT<\/code> and <code>ENOFILE<\/code>. The <code>errno.h<\/code> header may define one or both. If only one, delete the undefined entry from your code. The <code>errno<\/code> value is 2 either way.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>File renaming error: File not found<\/code><\/pre>\n<p>I didn&#8217;t include all the potential <code>errno<\/code> values for the <em>rename()<\/em> function in the code&#8217;s <em>switch-case<\/em> structure. Some of the errors are unusual or rare, so I opted for the <em>default<\/em> in the <em>switch-case<\/em> structure to catch those errors with an <code>Unknown error<\/code> message.<\/p>\n<p>If all this effort seems like too much work, it is! The C language offers yet another function for displaying error messages as well as automatically looking up <code>errno<\/code> values. I cover that function in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1740\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get more detail on common file access errors. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1735\">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-1735","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\/1735","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=1735"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1735\/revisions"}],"predecessor-version":[{"id":1767,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1735\/revisions\/1767"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}