Note that there are some explanatory texts on larger screens.

plurals
  1. POlisting conditionally an array of strings
    primarykey
    data
    text
    <p>I'm trying to list all files and folders in a given directory in C, the following code errors out and i cant figure out whats wrong </p> <pre><code>#include &lt;sys/types.h&gt; #include &lt;dirent.h&gt; #include &lt;regex.h&gt; #include &lt;stdio.h&gt; #include &lt;unistd.h&gt; #include &lt;pwd.h&gt; enum { WALK_OK = 0, WALK_BADPATTERN, WALK_BADOPEN, }; int walk_directories(const char *dir, const char *pattern, char* strings[]) { struct dirent *entry; regex_t reg; DIR *d; int i = 0; //char array[256][256]; if (regcomp(&amp;reg, pattern, REG_EXTENDED | REG_NOSUB)) return WALK_BADPATTERN; if (!(d = opendir(dir))) return WALK_BADOPEN; while (entry = readdir(d)) if (!regexec(&amp;reg, entry-&gt;d_name, 0, NULL, 0) ) //puts(entry-&gt;d_name); strings[i] = (entry-&gt;d_name); i++; closedir(d); regfree(&amp;reg); return WALK_OK; } void main() { struct passwd *pw = getpwuid(getuid()); char *homedir = pw-&gt;pw_dir; strcat(homedir, "/.themes"); int n = 0; char *array[256][100]; char *array2[256][100]; walk_directories(homedir, "", array); for (n = 0; n &lt; 256; n++) { //do stuff here later, but just print it for now printf ("%s\n", array[n]); } walk_directories("/usr/share/themes", "", array2); for (n = 0; n &lt; 256; n++) { //do stuff here later, but just print it for now printf ("%s\n", array2[n]); } } </code></pre> <p>The error at compile time is </p> <pre><code>test2.c: In function ‘main’: test2.c:42:2: warning: incompatible implicit declaration of built-in function ‘strcat’ [enabled by default] test2.c:48:2: warning: passing argument 3 of ‘walk_directories’ from incompatible pointer type [enabled by default] test2.c:15:5: note: expected ‘char **’ but argument is of type ‘char * (*)[100]’ test2.c:52:6: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat] test2.c:55:2: warning: passing argument 3 of ‘walk_directories’ from incompatible pointer type [enabled by default] test2.c:15:5: note: expected ‘char **’ but argument is of type ‘char * (*)[100]’ test2.c:59:6: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat] </code></pre> <p>If it helps, I've implemented what I want already in python, this is the desired result for C</p> <pre><code>import os DATA_DIR = "/usr/share" def walk_directories(dirs, filter_func): valid = [] try: for thdir in dirs: if os.path.isdir(thdir): for t in os.listdir(thdir): if filter_func(os.path.join(thdir, t)): valid.append(t) except: logging.critical("Error parsing directories", exc_info=True) return valid def _get_valid_themes(): """ Only shows themes that have variations for gtk+-3 and gtk+-2 """ dirs = ( os.path.join(DATA_DIR, "themes"), os.path.join(os.path.expanduser("~"), ".themes")) valid = walk_directories(dirs, lambda d: os.path.exists(os.path.join(d, "gtk-2.0")) and \ os.path.exists(os.path.join(d, "gtk-3.0"))) return valid print(_get_valid_themes()) </code></pre> <p>thank you</p> <p>[EDIT] thanks for the help, only problem im having now is the printf's all spit out rubbish instead of what i expected, ive tried a few things and the while loop looks like this now</p> <pre><code> while (entry = readdir(d)) if (!regexec(&amp;reg, entry-&gt;d_name, 0, NULL, 0) ) //printf("%s\n",entry-&gt;d_name); strcpy(strings[i], (entry-&gt;d_name)); //strings[i] = (entry-&gt;d_name); printf("%i\n",i); i++; closedir(d); </code></pre> <p>the i doesnt get printed properly either, this is all i get from the 3 printf statements</p> <pre><code>0 Adwaita2 \@ 0 Radiance �� \@ �K�� � `��� ���� � �� � .N= �O�� � � </code></pre> <p>should mention that if i enable</p> <pre><code> printf("%s\n",entry-&gt;d_name); </code></pre> <p>then it prints the expected output though</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload