<p>How do you scan a directory for folders and files in C? It needs to be cross-platform.</p>

[{'answer_id': 12500, 'author': 'PW.', 'author_id': 927, 'author_profile': 'https://Stackoverflow.com/users/927', 'pm_score': 3, 'selected': False, 'text': '<p>opendir/readdir are POSIX. If POSIX is not enough for the portability you want to achieve, check <a href="http://apr.apache.org/docs/apr/1.3/group__apr__dir.html" rel="noreferrer">Apache Portable Runtime</a></p>\n'}, {'answer_id': 12506, 'author': 'Clayton', 'author_id': 1449, 'author_profile': 'https://Stackoverflow.com/users/1449', 'pm_score': 6, 'selected': False, 'text': '<p>The following POSIX program will print the names of the files in the current directory:</p>\n<pre><code>#define _XOPEN_SOURCE 700\n#include &lt;stdio.h&gt;\n#include &lt;sys/types.h&gt;\n#include &lt;dirent.h&gt;\n\nint main (void)\n{\n  DIR *dp;\n  struct dirent *ep;     \n  dp = opendir (&quot;./&quot;);\n  if (dp != NULL)\n  {\n    while ((ep = readdir (dp)) != NULL)\n      puts (ep-&gt;d_name);\n          \n    (void) closedir (dp);\n    return 0;\n  }\n  else\n  {\n    perror (&quot;Couldn\'t open the directory&quot;);\n    return -1;\n  }\n}\n</code></pre>\n<p>Credit: <a href="http://www.gnu.org/software/libtool/manual/libc/Simple-Directory-Lister.html" rel="nofollow noreferrer">http://www.gnu.org/software/libtool/manual/libc/Simple-Directory-Lister.html</a></p>\n<p>Tested in Ubuntu 16.04.</p>\n'}, {'answer_id': 12507, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': 5, 'selected': False, 'text': '<p>The strict answer is "you can\'t", as the very concept of a folder is not truly cross-platform.</p>\n\n<p>On MS platforms you can use _findfirst, _findnext and _findclose for a \'c\' sort of feel, and FindFirstFile and FindNextFile for the underlying Win32 calls.</p>\n\n<p>Here\'s the C-FAQ answer:  </p>\n\n<p><a href="http://c-faq.com/osdep/readdir.html" rel="noreferrer">http://c-faq.com/osdep/readdir.html</a></p>\n'}, {'answer_id': 12513, 'author': 'Andrew Grant', 'author_id': 1043, 'author_profile': 'https://Stackoverflow.com/users/1043', 'pm_score': 4, 'selected': False, 'text': '<p>There is no standard C (or C++) way to enumerate files in a directory.</p>\n\n<p>Under Windows you can use the FindFirstFile/FindNextFile functions to enumerate all entries in a directory. Under Linux/OSX use the opendir/readdir/closedir functions.</p>\n'}, {'answer_id': 12694, 'author': 'Pascal', 'author_id': 1311, 'author_profile': 'https://Stackoverflow.com/users/1311', 'pm_score': 2, 'selected': False, 'text': "<p>Directory listing varies greatly according to the OS/platform under consideration. This is because, various Operating systems using their own internal system calls to achieve this.</p>\n\n<p>A solution to this problem would be to look for a library which masks this problem and portable. Unfortunately, there is no solution that works on all platforms flawlessly. </p>\n\n<p>On POSIX compatible systems, you could use the library to achieve this using the code posted by Clayton (which is referenced originally from the Advanced Programming under UNIX book by W. Richard Stevens). this solution will work under *NIX systems and would also work on Windows if you have Cygwin installed.</p>\n\n<p>Alternatively, you could write a code to detect the underlying OS and then call the appropriate directory listing function which would hold the 'proper' way of listing the directory structure under that OS.</p>\n"}, {'answer_id': 87178, 'author': 'Isak Savo', 'author_id': 8521, 'author_profile': 'https://Stackoverflow.com/users/8521', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://library.gnome.org/devel/glib/stable/" rel="noreferrer">GLib</a> is a portability/utility library for C which forms the basis of the GTK+ graphical toolkit. It can be used as a standalone library.</p>\n\n<p>It contains portable wrappers for managing directories. See <a href="http://library.gnome.org/devel/glib/stable/glib-File-Utilities.html" rel="noreferrer">Glib File Utilities</a> documentation for details.</p>\n\n<p>Personally, I wouldn\'t even consider writing large amounts of C-code without something like GLib behind me. Portability is one thing, but it\'s also nice to get data structures, thread helpers, events, mainloops etc. for free </p>\n\n<p>Jikes, I\'m almost starting to sound like a sales guy :) (don\'t worry, glib is open source (LGPL) and I\'m not affiliated with it in any way)</p>\n'}, {'answer_id': 6805559, 'author': 'user541686', 'author_id': 541686, 'author_profile': 'https://Stackoverflow.com/users/541686', 'pm_score': 2, 'selected': False, 'text': '<p>The most <em>similar</em> method to <code>readdir</code> is probably using the little-known <a href="http://msdn.microsoft.com/en-us/library/kda16keh.aspx" rel="nofollow"><code>_find</code> family of functions</a>.</p>\n'}, {'answer_id': 14679104, 'author': 'congusbongus', 'author_id': 2038264, 'author_profile': 'https://Stackoverflow.com/users/2038264', 'pm_score': 4, 'selected': False, 'text': '<p>I\'ve created an open source (BSD) C header that deals with this problem. It currently supports POSIX and Windows. Please check it out:</p>\n\n<p><a href="https://github.com/cxong/tinydir">https://github.com/cxong/tinydir</a></p>\n\n<pre><code>tinydir_dir dir;\ntinydir_open(&amp;dir, "/path/to/dir");\n\nwhile (dir.has_next)\n{\n    tinydir_file file;\n    tinydir_readfile(&amp;dir, &amp;file);\n\n    printf("%s", file.name);\n    if (file.is_dir)\n    {\n        printf("/");\n    }\n    printf("\\n");\n\n    tinydir_next(&amp;dir);\n}\n\ntinydir_close(&amp;dir);\n</code></pre>\n'}, {'answer_id': 47539331, 'author': 'Howard Wu', 'author_id': 833657, 'author_profile': 'https://Stackoverflow.com/users/833657', 'pm_score': 0, 'selected': False, 'text': '<p>You can find the sample code on the <a href="https://en.wikibooks.org/wiki/C_Programming/dirent.h" rel="nofollow noreferrer">wikibooks link</a></p>\n\n<pre><code>/**************************************************************\n * A simpler and shorter implementation of ls(1)\n * ls(1) is very similar to the DIR command on DOS and Windows.\n **************************************************************/\n#include &lt;stdio.h&gt;\n#include &lt;dirent.h&gt;\n\nint listdir(const char *path) \n{\n  struct dirent *entry;\n  DIR *dp;\n\n  dp = opendir(path);\n  if (dp == NULL) \n  {\n    perror("opendir");\n    return -1;\n  }\n\n  while((entry = readdir(dp)))\n    puts(entry-&gt;d_name);\n\n  closedir(dp);\n  return 0;\n}\n\nint main(int argc, char **argv) {\n  int counter = 1;\n\n  if (argc == 1)\n    listdir(".");\n\n  while (++counter &lt;= argc) {\n    printf("\\nListing %s...\\n", argv[counter-1]);\n    listdir(argv[counter-1]);\n  }\n\n  return 0;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/12489', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/432/']