{"id":3246,"date":"2018-08-25T00:01:20","date_gmt":"2018-08-25T07:01:20","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3246"},"modified":"2018-09-01T12:45:08","modified_gmt":"2018-09-01T19:45:08","slug":"reading-a-directory","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3246","title":{"rendered":"Reading a Directory"},"content":{"rendered":"<p>I&#8217;m refusing to call it a &#8220;folder.&#8221; That nonsense gained popularity with the Macintosh and then Windows. Before then, it was a <em>directory<\/em>, a list of files stored on media. Special C language functions are available to read and manipulate directories, which helps your programs manage files and do other fun file stuff.<br \/>\n<!--more--><br \/>\nA directory is really a special type of file, a data container that acts as a database referencing other files stored on the media. The media&#8217;s file system determines how the files are organized and accessed. The directory holds all that information, such as the file&#8217;s physical location, its name, timestamps, permissions, and other trivia. These details are accessible when you use the proper C language functions.<\/p>\n<p>To access a directory, use the <em>opendir()<\/em> function. It&#8217;s prototyped in the <code>dirent.h<\/code> header file as:<\/p>\n<p><code>DIR *opendir(const char *filename);<\/code><\/p>\n<p>The function requires a string argument, a name or path to a directory. The value returned is a <em>DIR<\/em> pointer, similar to the <em>FILE<\/em> pointer returned by <em>fopen()<\/em>.<\/p>\n<p>After opening the directory and doing whatever, you use the <em>closedir()<\/em> function to close the directory:<\/p>\n<p><code>int closedir(DIR *dirp);<\/code><\/p>\n<p>The function requires a <em>DIR<\/em> pointer (<code>dirp<\/code>, heh) and returns 0 upon success, otherwise -1. Use the <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1735\"><code>errno<\/code><\/a> global variable to further examine the issue when -1 is returned.<\/p>\n<p>In the following code, the current directory (abbreviated as <code>.<\/code> ) is opened and closed:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;dirent.h&gt;\r\n\r\nint main()\r\n{\r\n    DIR *folder;\r\n\r\n    folder = opendir(\".\");\r\n    if(folder == NULL)\r\n    {\r\n        puts(\"Unable to read directory\");\r\n        return(1);\r\n    }\r\n    else\r\n    {\r\n        puts(\"Directory is opened!\");\r\n    }\r\n    closedir(folder);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>DIR<\/em> pointer variable <code>folder<\/code> acts as the directory handle for the <em>opendir()<\/em> function in Line 8. If it&#8217;s <code>NULL<\/code>, the program quits. Otherwise a simple string is output and the directory is closed in Line 18. Here&#8217;s sample output:<\/p>\n<p><code>Directory is opened!<\/code><\/p>\n<p>Opening and closing a directory is boring, but I like to build upon code when presenting a new concept. To expand the program and make it useful, the <em>readdir()<\/em> function is added. Here&#8217;s how it&#8217;s prototyped, also in the <code>dirent.h<\/code> header file:<\/p>\n<p><code>struct dirent *readdir(DIR *dirp);<\/code><\/p>\n<p>Structure variable <code>dirent<\/code> is a pointer that contains information about a specific entry read from a directory, referenced by the <em>DIR<\/em> pointer used in the <em>readdir()<\/em> function. This function is called repeatedly until <code>NULL<\/code> is returned, indicating no further entries are available in the directory.<\/p>\n<p>The <code>dirent<\/code> structure is customized based on the file system. A key member worthy of access is <code>d_name<\/code>, which represent the directory entry&#8217;s filename. The following code demonstrates how the <code>d_name<\/code> structure member can be used when reading a directory:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;dirent.h&gt;\r\n\r\nint main()\r\n{\r\n    DIR *folder;\r\n    struct dirent *entry;\r\n    int files = 0;\r\n\r\n    folder = opendir(\".\");\r\n    if(folder == NULL)\r\n    {\r\n        perror(\"Unable to read directory\");\r\n        return(1);\r\n    }\r\n\r\n    while( (entry=readdir(folder)) )\r\n    {\r\n        files++;\r\n        printf(\"File %3d: %s\\n\",\r\n                files,\r\n                entry-&gt;d_name\r\n              );\r\n    }\r\n\r\n    closedir(folder);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code is the same as the previous example, with a <em>while<\/em> loop inserted to read the directory entries. The loop spins as long as the value returned from <em>readdir()<\/em> isn&#8217;t <code>NULL<\/code>. For each item found, <em>int<\/em> variable <code>files<\/code> tracks its sequential number (which is part of this code and not part of the file system) and <code>entry-&gt;d_name<\/code> prints its name:<\/p>\n<pre><code>File   1: .\r\nFile   2: ..\r\nFile   3: 0825a.c\r\nFile   4: 0825b.c\r\nFile   5: 08exercise-a.c\r\nFile   6: 08exercise-b.c\r\nFile   7: 0901.c\r\nFile   8: 0908.c\r\nFile   9: a.out\r\nFile  10: gettysburg.txt\r\nFile  11: sto<\/code><\/pre>\n<p>Directory entries <code>.<\/code> and <code>..<\/code> represent the current and parent directories. The other items are files, though I know that <code>sto<\/code> is another directory. In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3252\">next week&#8217;s Lesson<\/a>, I discuss how to differentiate between standard files and directory entries.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Directories are read like files, but with special rules, restrictions, and formats. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3246\">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-3246","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\/3246","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=3246"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3246\/revisions"}],"predecessor-version":[{"id":3278,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3246\/revisions\/3278"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}