Note that there are some explanatory texts on larger screens.

plurals
  1. POWeird bug with routine to list files recursively
    text
    copied!<p>Can anybody spot what I am missing here? This block of code is supposed to recursively access directories and store the directory path and the path of the files in it. It will fprintf() a true if the file is a directory and a false if the file is not a directory. Weird part is that the printf routine for fileName works fine but when its time to fprintf fileName to a file, it just prints a newline where its supposed to print fileName. </p> <pre><code>/* List the files in "dir_name". */ static void listDir(const char *dirName) { DIR *dir; /* * Open the directory specified by "dirName". */ dir = opendir(dirName); /* * Check it was opened. */ if (!dir) { fprintf(stderr, "Cannot open directory '%s': %s\n", dirName, strerror(errno)); exit(EXIT_FAILURE); } while (1) { struct dirent *entry; const char *dir_name; /* * "Readdir" gets subsequent entries from "d". */ entry = readdir(dir); if (!entry) { /* * There are no more entries in this directory, so break out of the while loop. */ break; } dir_name = entry-&gt;d_name; char fileName[PATH_MAX]; // Assign fileName to path if the file is not a directory if (entry-&gt;d_type != DT_DIR) { if (strcmp(dirName, "/") != 0) { snprintf(fileName, PATH_MAX, "%s/%s", dirName, dir_name); } else { snprintf(fileName, PATH_MAX, "%s%s", dirName, dir_name); } } /* Access directory and leave out /. and /.. in the process */ if (entry-&gt;d_type == DT_DIR) { /* * Check that the directory is not "d" or d's parent. */ if (strcmp(dir_name, "..") != 0 &amp;&amp; strcmp(dir_name, ".") != 0) { int path_length; char path[PATH_MAX], indexPath[PATH_MAX]; if (strcmp(dirName, "/") != 0) { path_length = snprintf(path, PATH_MAX, "%s/%s", dirName, dir_name); } else { path_length = snprintf(path, PATH_MAX, "%s%s", dirName, dir_name); } strcpy(indexPath, path); strcat(indexPath, "/masterIndex.db"); FILE *fp; if ((fp = fopen(indexPath, "a")) == NULL) { printf("Cannot open file\n"); return; } printf("File: %s\n (TRUE)\n", path); printf("File: %s\n (FALSE)\n", fileName); // This routine prints fileName correctly fprintf(fp, "%s\n", path); fprintf(fp, "%s\n", "true"); fprintf(fp, "%s\n", fileName); // This routine prints a newline where fileName is supposed to be fprintf(fp, "%s\n", "false"); fclose(fp); // Activate this for screw ups /* char command[PATH_MAX]; strcpy(command, "cd "); strcat(command, path); strcat(command, " &amp;&amp; rm abc *.finderDB .DS_Store"); printf("%s\n", command); system(command);*/ if (path_length &gt;= PATH_MAX) { fprintf(stderr, "Path length has gotten too long.\n"); exit(EXIT_FAILURE); } /* * Recursively call "list_dir" with the new path. */ listDir(path); } } } /* * After going through all the entries, close the directory. */ if (closedir(dir)) { fprintf(stderr, "Could not close '%s': %s\n", dirName, strerror(errno)); exit(EXIT_FAILURE); } } </code></pre>
 

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