Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is wrong with this example from K&R?
    primarykey
    data
    text
    <p>I am trying the listing directories example from K&amp;R section 8.6, but when testing the code it, says "can't access" for every single file in the directory. It is because it's designed for Unix? Below is what I think the code should look like when pieced together, what am I missing? <strong>ADD:How do I fix this code to make it work?</strong></p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/stat.h&gt; #include "dirent.h" #include &lt;sys/dir.h&gt; #define NAME_MAX 14 typedef struct { long ino; char name[NAME_MAX+1]; } Dirent; void fsize(char *); /* print file sizes */ main(int argc, char **argv) { if (argc == 1) fsize("."); else while (--argc &gt; 0) fsize(*++argv); return 0; } void dirwalk(char *, void (*fcn)(char *)); /* fsize: print size of file "name" */ void fsize(char *name) { struct stat stbuf; if (stat(name, &amp;stbuf) == -1) { fprintf(stderr, "fsize: can't access %s\n", name); return; } if ((stbuf.st_mode &amp; S_IFMT) == S_IFDIR) dirwalk(name, fsize); printf("%81d %s\n", stbuf.st_size, name); } #define MAX_PATH 1024 /* dirwalk: apply fn to all files in dir */ void dirwalk(char *dir, void (*fcn)(char *)) { char name[MAX_PATH]; Dirent *dp; DIR *dfd; if ((dfd = opendir(dir)) == NULL) { fprintf(stderr, "dirwalk: can't open %s\n", dir); return; } while ((dp = readdir(dfd)) != NULL) { if (strcmp(dp-&gt;name, ".") == 0 || strcmp(dp-&gt;name, "..") == 0) continue; if (strlen(dir)+strlen(dp-&gt;name)+2 &gt; sizeof(name)) fprintf(stderr, "dirwalk: name %s/%s too long\n", dir, dp-&gt;name); else { sprintf(name, "%s/%s", dir, dp-&gt;name); (*fcn)(name); } } closedir(dfd); } </code></pre>
    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.
    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