{"id":3463,"date":"2019-01-26T00:01:51","date_gmt":"2019-01-26T08:01:51","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3463"},"modified":"2019-01-19T10:57:47","modified_gmt":"2019-01-19T18:57:47","slug":"does-the-file-exist","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3463","title":{"rendered":"Does the File Exist?"},"content":{"rendered":"<p>Many programs rely upon external files to store data. When the file is missing, or not where it should be, the <em>fopen()<\/em> function returns a <code>NULL<\/code> pointer, which your code can act upon. Or, you can pre-check for the file&#8217;s existence by using the <em>access()<\/em> function.<br \/>\n<!--more--><br \/>\nThe <em>access()<\/em> function does more than just check to see if a file is present. It can also check the file&#8217;s type and permissions. The function is prototyped in the <code>unistd.h<\/code> header file. Here is the man page format:<\/p>\n<p><code>int access(const char *path, int mode);<\/code><\/p>\n<p>The <em>path<\/em> is the filename or the complete path to a file or directory. The <em>mode<\/em> argument determines the file&#8217;s permissions by using constant expressions and logical operations. Here are the constant expressions defined in <code>unistd.h<\/code>:<\/p>\n<table width=\"50%\" border=\"0\">\n<tr>\n<th>Constant<\/th>\n<th>Test<\/th>\n<\/tr>\n<tr>\n<td align=\"center\"><code>F_OK<\/code><\/td>\n<td>Does the file exist?<\/td>\n<\/tr>\n<tr>\n<td align=\"center\"><code>R_OK<\/code><\/td>\n<td>Does the file have read permission?<\/td>\n<\/tr>\n<tr>\n<td align=\"center\"><code>W_OK<\/code><\/td>\n<td>Does the file have write permission?<\/td>\n<\/tr>\n<tr>\n<td align=\"center\"><code>X_OK<\/code><\/td>\n<td>Does the file have execute or search permission?<\/td>\n<\/tr>\n<\/table>\n<p>When the <em>access()<\/em> function is successful, zero is returned, -1 otherwise. The <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1735\"><em>errno<\/em><\/a> variable is set when -1 is returned.<\/p>\n<p>The following code demonstrates the <em>access()<\/em> function as a test on the existence of the file <a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/09\/gettysburg.txt\">gettysburg.txt<\/a>, which is assumed to be in the same directory as the program:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main()\r\n{\r\n    const char filename[] = \"gettysburg.txt\";\r\n    int r;\r\n\r\n    r = access(filename,F_OK);\r\n    if( r == 0 )\r\n        printf(\"File '%s' exists\\n\",filename);\r\n    else\r\n        printf(\"File '%s' cannot be found\\n\",filename);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>access()<\/em> function at Line 9 does not open the file. It&#8217;s just a test. Likewise, with any of the <em>mode<\/em> arguments, the file isn&#8217;t opened. Sometimes such a thing is necessary in your code. And if you use this function, ensure that you properly inform the user of a file&#8217;s condition, how it affects the program, and what the user can do to remedy the situation.<\/p>\n<p>Finally, the <em>access()<\/em> function has some issues worthy of concern. For example, when checking file permissions remember that they can change as your program runs. Some permission issues can affect system security. Refer to the <em>access()<\/em> function&#8217;s man page for specific details.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If your code relies upon an external file, it&#8217;s good to know that the file exists before it&#8217;s accessed. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3463\">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-3463","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\/3463","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=3463"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3463\/revisions"}],"predecessor-version":[{"id":3468,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3463\/revisions\/3468"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}