{"id":1740,"date":"2016-02-06T00:01:07","date_gmt":"2016-02-06T08:01:07","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1740"},"modified":"2016-01-30T09:38:50","modified_gmt":"2016-01-30T17:38:50","slug":"display-errors-and-errno-messages-automagically","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1740","title":{"rendered":"Display Errors and errno Messages Automagically"},"content":{"rendered":"<p>Last week&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1735\">Lesson<\/a> discussed the <code>errno<\/code> variable and how it can display more detailed error messages. I have two updates to that Lesson.<br \/>\n<!--more--><br \/>\nThe first is that error messages are best directed to the <code>stderr<\/code> device. It&#8217;s the ugly stepsister of the C language device constants, running a distant third to <code>stdin<\/code> (standard input) and <code>stdout<\/code> (standard output).<\/p>\n<p>The reason for the <code>stderr<\/code> (standard error output) device is to ensure that a program&#8217;s output isn&#8217;t redirected to another device. The output to <code>stderr<\/code> is always sent to the console (the screen) regardless of what other I\/O manipulation is taking place outside the program. Bottom line:<\/p>\n<p>Always send error messages to the <code>stderr<\/code> device.<\/p>\n<p>Here&#8217;s the update to last week&#8217;s code example:<\/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        fprintf(stderr,\"File renaming error: \");\r\n        switch(errno)\r\n        {\r\n            case EPERM:\r\n                fprintf(stderr,\"Operation not permitted\\n\");\r\n                break;\r\n            case ENOENT:\r\n            case ENOFILE:\r\n                fprintf(stderr,\"File not found\\n\");\r\n                break;\r\n            case EACCES:\r\n                fprintf(stderr,\"Permission denied\\n\");\r\n                break;\r\n            case ENAMETOOLONG:\r\n                fprintf(stderr,\"Filename is too long\\n\");\r\n                break;\r\n            default:\r\n                fprintf(stderr,\"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>The only modification is that all error-output functions were changed from <em>printf()<\/em> to <em>fprintf()<\/em> and the <code>stderr<\/code> device was added to the function&#8217;s arguments. That ensures that error messages are output to the standard error device. On modern computers, that means the messages always appear on the terminal window.<\/p>\n<p>The second update involves using a new function. That function handles the <code>errno<\/code> error code lookup, error message generation, and output to the standard error device. The function is called <em>perror()<\/em>, which I pronounce <em>Pierre<\/em>.<\/p>\n<p>The <em>perror()<\/em> function is defined in the <code>stdio.h<\/code> header file. Its argument is a text string displayed to the standard error device. If an <code>errno<\/code> value is generated, a specific error message appears after that text, followed by a newline (<code>\\n<\/code>).<\/p>\n<p>Here is an update to the previous code example, but using the <em>perror()<\/em> function:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.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        perror(\"File renaming error\");\r\n        return(1);\r\n    }\r\n    puts(\"File renamed\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code is much shorter, yet it does the same thing. Here is sample output when the <code>this.txt<\/code> file doesn&#8217;t exist:<\/p>\n<pre><code>File renaming error: No such file or directory<\/code><\/pre>\n<p>The <em>perror()<\/em> function displays its text argument (no newline). That output is followed by a colon, space, and an error message with a newline.<\/p>\n<p>The only sad news is that not every C language library implements the <em>perror()<\/em> function. If not, then you can use the <em>fprintf() <\/em>function to output to the <code>stderr<\/code> device, then use the <code>errno<\/code> variable to help dish up a specific error message.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The miraculous <em>perror()<\/em> function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1740\">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-1740","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\/1740","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=1740"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1740\/revisions"}],"predecessor-version":[{"id":1756,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1740\/revisions\/1756"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}