c - 带有例程的奇怪错误以递归方式列出文件

标签 c unix

有人能发现我在这里遗漏了什么吗?该代码块应该递归访问目录并将目录路径和文件路径存储在其中。如果文件是目录,则 fprintf() 为真,如果文件不是目录,则为假。奇怪的是,fileName 的 printf 例程工作正常,但是当它需要 fprintf fileName 到文件时,它只是在应该打印 fileName 的地方打印一个换行符。

/* 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->d_name;
    char fileName[PATH_MAX];
            // Assign fileName to path if the file is not a directory
    if (entry->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->d_type == DT_DIR) {

        /* 
         * Check that the directory is not "d" or d's parent. 
         */

        if (strcmp(dir_name, "..") != 0 && 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, " && rm abc *.finderDB .DS_Store");
            printf("%s\n", command);
            system(command);*/

            if (path_length >= 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);
}
}

最佳答案

system("cd any_path") 不会对调用进程产生任何影响;使用 chdir() 代替..

关于c - 带有例程的奇怪错误以递归方式列出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16942682/

相关文章:

c++ - 简单的 Makefile 问题(具有简单的依赖性)

linux - 如何启动多个 xterm 窗口并在每个窗口上运行命令,然后让每个窗口保持打开状态?

linux - 如何将函数/别名定义写入 Bash 历史记录?

c - 是否可以将 FILE * 存储在用户定义的结构中?

c++ - 测试 DLL 函数的最佳方法是什么?

c - .c 中的操作系统进程

c - 最小化 C 程序中的内存占用

linux - 按字段排序 UNIX

c - EAGAIN 是什么意思?

c - 计算 C 中结构的大小(数据对齐问题)