{"id":583,"date":"2012-10-18T14:51:40","date_gmt":"2012-10-18T14:51:40","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=583"},"modified":"2020-07-23T12:58:13","modified_gmt":"2020-07-23T12:58:13","slug":"working-with-directories","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/file-input-and-output\/working-with-directories\/working-with-directories","title":{"rendered":".Working With Directories"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Directory Locations<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Referenced from root<\/h5>\n\n\n\n<p>Use &#8220;\/&#8221;, e.g. &#8220;\/home\/pi\/my_directory_name&#8221;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Create Directory If It Doesn&#8217;t Exist<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h>\n#include &lt;sys\/stat.h>\n#include &lt;errno.h>\n#include &lt;stdio.h>\n\t\n\t\/\/----- CREATE DIRECTORY IF IT DOESN'T EXIST -----\n\tint result;\n\textern int errno;\n\tstruct stat sb;\n\tconst char *directory_name = \"\/home\/pi\/my_directory_name\";\n\n\tresult = stat(directory_name, &amp;sb);\n\tif ((result != 0) || !(sb.st_mode &amp; S_IFDIR))\n\t{\n\t\tif (errno == ENOENT)\t\t\/\/ENOENT=No such directory entry\n\t\t{\n\t\t\tprintf(\"The directory does not exist. Creating new directory: %s\\n\", directory_name);\n\t\t\t\/\/ Add more flags to the mode if necessary.\n\t\t\tresult = mkdir(directory_name, S_IRWXU);\n\t\t\tif (result != 0)\n\t\t\t{\n\t\t\t\tprintf(\"mkdir failed; errno=%d\\n\",errno);\n\t\t\t}\n\t\t}\n\t}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">List contents of a directory<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;dirent.h>\n\n\tDIR *directory1;\n\tstruct dirent *entry1;\n\n\tdirectory1 = opendir(DB_FILEIO_DIRECTORY_LIVE_UPDATES_NOT_ACKNOWLEDGED);\t\t\t\/\/Open a directory stream\n\tif (directory1 != NULL)\n\t{\n\t\twhile (entry1 = readdir(directory1))\n\t\t{\n\t\t\tif (entry1->d_type == DT_REG)\t\t\t\/\/DT_REG=regular file, DT_DIR=directory\n\t\t\t{\n\t\t\t\tprintf(\"File: %s\\n\", entry1->d_name);\n\t\t\t\n\t\t\t\t\/\/Getting separate filename and extension:\n\t\t\t\t\/*\n\t\t\t\tstring filename;\n\t\t\t\tstring filename_name;\n\t\t\t\tstring filename_extension;\n\t\t\t\tfilename = entry1->d_name;\n\t\t\t\tsize_t dot = filename.find_last_of(\".\");\n\t\t\t\tif (dot != std::string::npos)\n\t\t\t\t{\n\t\t\t\t\tfilename_name = filename.substr(0, dot);\n\t\t\t\t\tfilename_extension  = filename.substr(dot, filename.size() - dot);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tfilename_name = filename;\n\t\t\t\t\tfilename_extension  = \"\";\n\t\t\t\t}\n\t\t\t\tprintf(\"  Filename: %s\\n\", filename_name.c_str());\n\t\t\t\tprintf(\"  Extension: %s\\n\", filename_extension.c_str());\n\t\t\t\t*\/\n\t\t\t}\n\t\t\t\n\t\t}\n\t}\n\tclosedir(directory1);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Directory Locations Referenced from root Use &#8220;\/&#8221;, e.g. &#8220;\/home\/pi\/my_directory_name&#8221; Create Directory If It Doesn&#8217;t Exist List contents of a directory<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[159],"tags":[],"class_list":["post-583","post","type-post","status-publish","format-standard","hentry","category-working-with-directories"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=583"}],"version-history":[{"count":9,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/583\/revisions"}],"predecessor-version":[{"id":3289,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/583\/revisions\/3289"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}