简体   繁体   English

为什么我的程序不显示任何输出?

[英]Why is my program not displaying any output?

I am trying to recursively traverse through a directory and list any files that end in .txt or the names of folders.我正在尝试递归遍历目录并列出以 .txt 结尾的任何文件或文件夹的名称。

void listFilesRecursively(char *basePath) {

    char path[1000];
    struct dirent *ptr;
    DIR *dir = opendir(basePath);

    // Unable to open directory
    if (!dir)
        return;

    while ((ptr = readdir(dir)) != NULL) 
    {
        if (strcmp(ptr->d_name,".") != 0 && strcmp(ptr->d_name, "..") != 0)
        {

            char *name = NULL;
            char *type = NULL;

            while (name != "." || name != NULL) {
                name++;
            }

            for (name = ptr->d_name; *name != '\0' ; name++) {

                if (name == '.') {
                    while (*name != '\0') {
                        *type = *name;
                        name++;
                    }
                        break;                      

                } else if (*name == '\0') {
                    printf("%s\n", ptr->d_name);
                }

            }                   

            if (strcmp(type,"txt") == 0)            
                printf("%s\n", ptr->d_name);

            // create new path from our base path
            strcpy(path, basePath);
            strcat(path, "/");
            strcat(path, ptr->d_name);

            listFilesRecursively(path);

        }
    }
        closedir(dir);
}

My expected results would be the files that end in .txt and files that are folders.我的预期结果是以 .txt 结尾的文件和作为文件夹的文件。 However, the output is blank and it may go into an infinite loop.但是,输出为空白,并且可能进入无限循环。

This is an infinite loop:这是一个无限循环:

        while (name != "." || name != NULL) {
            name++;
        }

name is a char* . name是一个char* Unless the interned string literal "."除非实习字符串文字"." is stored at the NULL address (it isn't), then you're saying the loop should continue forever, because name will always be either not pointing to the storage for "."存储在NULL地址(不是),然后您说循环应该永远继续,因为name将始终不指向"."的存储"." or not pointing to NULL .或不指向NULL While I believe it's officially undefined behavior to compare unrelated pointers like "."虽然我认为比较像"."这样不相关的指针是官方未定义的行为"." and name , in practice most (all flat memory model?) compilers just allow arbitrary pointer comparisons, and since you never dereference the pointer, you never actually try to read the unallocated addresses you're traversing (you'd eventually segfault if you did), so it just goes on forever.name ,实际上大多数(所有平面内存模型?)编译器只允许任意指针比较,并且由于您从不取消引用指针,因此您实际上从未尝试读取您正在遍历的未分配地址(如果您这样做,您最终会出现段错误),所以它会永远持续下去。

I'm unclear on what the goal of that loop is (maybe stripping leading . s from "hidden" file names?), so I can't really suggest a fix other than removing it.我不清楚该循环的目标是什么(也许从“隐藏的”文件名中去除前导. s ?),所以除了删除它之外,我真的不能建议修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的计算均方误差的程序没有给出任何 output? - Why is my program to calculate Mean square error not giving any output? 为什么我的程序没有显示任何 output? 0 个错误,0 个警告但没有 output? 我正在使用 Dev C++ 编译器 - Why is my program not showing any output? 0 errors, 0 warnings but no output? I'm using Dev C++ compiler 为什么我的程序不显示每个 output? 2次迭代后停止 - Why isn't my program displaying every output? It stops after 2 iterations 为什么这个简单的 C 程序没有显示任何输出? - Why is this simple C program not showing any output? 为什么我的C程序在每次执行时都更改输出而没有任何输入或代码更改? - Why does my C program changes the output in each execution without any input or code change? 为什么我的代码在 output 中显示奇怪的数字? - Why is my code displaying bizzare numbers in the output? 为什么我的C程序会输出这个? - Why does my C program output this? 为什么我的 C 程序没有给出正确的输出? - Why is my C program not giving correct output? 为什么我的pthread和semaphore程序的output是空白的? - Why is the output of my pthread and semaphore program blank? 为什么printf会在我的程序中输出? - Why does printf output this in my program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM