Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursively list directories in C
    primarykey
    data
    text
    <p>im looking for a code who will recursively list all directories and files of a directory given on argument in C programming, ive find an interresting code (below) but I dont understand the snprintf function and particulary the "/", I would prefer to use strcat or other system function to override the sprintf function but I dont see how since I dont understand what the snprintf do here. Heres the code:</p> <pre><code>int is_directory_we_want_to_list(const char *parent, char *name) { struct stat st_buf; if (!strcmp(".", name) || !strcmp("..", name)) return 0; char *path = alloca(strlen(name) + strlen(parent) + 2); sprintf(path, "%s/%s", parent, name); stat(path, &amp;st_buf); return S_ISDIR(st_buf.st_mode); } int list(const char *name) { DIR *dir = opendir(name); struct dirent *ent; while (ent = readdir(dir)) { char *entry_name = ent-&gt;d_name; printf("%s\n", entry_name); if (is_directory_we_want_to_list(name, entry_name)) { // You can consider using alloca instead. char *next = malloc(strlen(name) + strlen(entry_name) + 2); sprintf(next, "%s/%s", name, entry_name); list(next); free(next); } } closedir(dir); } </code></pre> <p>from <a href="https://stackoverflow.com/questions/8436841/how-to-recursively-list-directories-in-c-on-linux">How to recursively list directories in C on LINUX</a></p> <p>Okay, my program is running but now i want to save all the file and directory printed to a file like i run my program ./a.out . > buffer where buffer contain what the program should print on the shell </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.
 

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