首页 > 解决方案 > 如何打印出每个目录但作为路径名?(在身体描述中解释)

问题描述

这是由 Lloyd Macrohon 编写的代码,所有功劳都属于他,但是在过去的两天里,我一直在尝试修改此代码,以便我不想显示目录中每个项目的列表,而是要修改它将每个项目显示为长路径名。

#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>

void listdir(const char *name, int indent)
{
    DIR *dir;
    struct dirent *entry;

    if (!(dir = opendir(name)))
        return;

    while ((entry = readdir(dir)) != NULL) {
        if (entry->d_type == DT_DIR) {
            char path[1024];
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
                continue;
            snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
            printf("%*s[%s]\n", indent, "", entry->d_name);
            listdir(path, indent + 2);
        } else {
            printf("%*s- %s\n", indent, "", entry->d_name);
        }
    }
    closedir(dir);
}

int main(void) {
    listdir(".", 0);
    return 0;
}

以上是在 unix 终端中运行时会输出如下内容的原始代码:

-file
[directory]
    [directory]
        -file
    -file
    -file
    ....

但相反,我试图像这样运行它:

file
directory/directory/file
directory/file
directory/file
...

我的代码版本已经删除了意图,并用一个字符替换了它们,该字符包含一个字符,该字符应该是文件之前的路径名。

#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>


void listdir(const char *name,const char *pname)
{
    DIR *dir;
    struct dirent *entry;
    char pathn = pname;

    if (!(dir = opendir(name)))
        return;

    while ((entry = readdir(dir)) != NULL) {
        if (entry->d_type == DT_DIR) {
            char path[1024];
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
                continue;
            snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
            //printf("%s/", entry->d_name);
            pathn = pathn + entry->d_name;
            listdir(path,pathn);
        }
        else if( pathn != ""){
          printf("%s and  %s ", pathn, entry->d_name);
        }
        else {
            printf("%s\n", entry->d_name);
        }
    }
    closedir(dir);
}

int main(void) {
  listdir(".","");
    return 0;
}

注意:另外请原谅我可能错过的任何规定,我不知道未经他们许可修改/上传其他用户代码是否违法或违反规则,我对此还是很陌生。

标签: crecursiondirectory

解决方案


有什么理由不打印name传递给函数的内容吗?

#define _GNU_SOURCE 1
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>

void listdir(const char *name, int indent)
{
    DIR *dir;
    struct dirent *entry;

    if (!(dir = opendir(name)))
        return;

    while ((entry = readdir(dir)) != NULL) {
        if (entry->d_type == DT_DIR) {
            char path[1024];
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
                continue;
            snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
            //printf("%*s[%s]\n", indent, "", entry->d_name);
            listdir(path, indent + 2);
        } else {
            printf("%s/%s\n", name, entry->d_name);
        }
    }
    closedir(dir);
}

int main() {
    system("mkdir -p dir/dir; touch dir/file dir/dir/file");
    listdir(".", 0);
    return 0;
}

可在onlinedbg获得实时代码。


推荐阅读