Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C获取递归文件的内容_C_File_Recursion_Printing - Fatal编程技术网

C获取递归文件的内容

C获取递归文件的内容,c,file,recursion,printing,C,File,Recursion,Printing,我试图递归地打印给定目录的每个文件的内容。我的代码中有一些逻辑错误,我无法找出哪里出了问题 浏览目录/子目录并获取文件名: char filename[500], filepath[500]; void listdir(char *dir) { DIR *dp; struct dirent *name; struct stat statbuf; if((dp = opendir(dir)) == NULL) { fprintf(std

我试图递归地打印给定目录的每个文件的内容。我的代码中有一些逻辑错误,我无法找出哪里出了问题

浏览目录/子目录并获取文件名:

char filename[500], filepath[500];

void listdir(char *dir)
{
    DIR *dp;
    struct dirent *name;
    struct stat statbuf;

    if((dp = opendir(dir)) == NULL) 
    {
        fprintf(stderr,"cannot open directory: %s\n", dir);
        return;
    }
    chdir(dir);

    while((name = readdir(dp)) != NULL) 
    {
        if(lstat(name->d_name, &statbuf) == 0)
        {
            if(statbuf.st_mode & S_IFDIR)
            {
                /* Found a directory, but ignore . and .. */
                if(strcmp(".", name->d_name) == 0 || strcmp("..", name->d_name) == 0)
                    continue;

                // Directory name
                int len = strlen(filepath);
                strcat(filepath, name->d_name);
                strcat(filepath, "/");

                /* Recurse at a new indent level */
                listdir(name->d_name);

                filepath[len] = '\0';
            }
            else
            {
                // Filename
                strcpy(filename, filepath);
                strcat(filename, name->d_name);

                // Prevent double printing
                if(file[strlen(filename) - 1] != '~')
                    readFile(filename);
            }
        }
    }

    chdir("..");
    closedir(dp);
}
打开/读取文件内容:

void readFile(char *filepath)
{
    char ch;
    FILE *file;

    file = fopen(filepath, "r");

    if(file)
    {
        while((ch = fgetc(file)) != EOF)
            printf("%c", ch);

        fclose(file);
    }
}
主要内容:

因此,如果我像这样运行程序
/fileread~/Desktop
,并且我的桌面上有这个:

Songs.txt、test/test.txt、test/random.txt

它将打印Songs.txt的内容,但不会打印测试文件夹中的任何内容。如果我取出“防止双重打印”行,它将打印Songs.txt的内容两次,但仍然不会打印测试文件夹中的任何内容

我做错了什么?任何帮助都将不胜感激


已更新

我添加了本地字符串:

void getdir(char *dir)
{
    char *direct;
    char filepath[250];

    ...

    direct = malloc(strlen(dir) + strlen(name->d_name) + 2);
    strcpy(direct, dir);
    strcat(direct, "/");
    strcat(direct, name->d_name);

    getdir(direct);
    free(direct);
}
else
{
    // Concatenate file name
    strcpy(filepath, dir);
    strcat(filepath, "/");
    strcat(filepath, name->d_name);

    if(filepath[strlen(filepath) - 1] != '~')
        readFile(filepath);
}

这似乎很有效。如果有什么我不该做的,请告诉我。谢谢大家的帮助

您正在摆弄全局字符串,因此当递归返回到上一个/更低的级别时,它们不再是相同的。您需要复制一个本地定义的字符串,使用并传递它。无论opendir()是否成功,我都会尝试在开始时打印'dir'参数。@WeatherVane哦。。忽略了这一点,可能是因为这是一件奇怪的事情——看不出比局部变量有什么优势:(是的,当你处理递归函数时,你要么管理局部变量,要么——如果是全局的——使用堆栈,这样你就可以“弹出”离开时。还有,OP,这些全局变量完全fu..安全地破坏了你的线程。你在摆弄全局字符串,因此当递归返回到上一个/更低的级别时,它们不再是相同的。你需要复制一个本地定义的字符串,使用并传递它。我会尝试在开始时打印'dir'参数,而不是matter opendir()是否成功。@WeatherVane哦..错过了这一点,可能是因为这是一件奇怪的事情-看不出比局部变量有任何优势:(是的,当处理递归函数时,你要么管理局部变量,要么-如果是全局的-使用堆栈,这样你就可以“弹出”当你离开的时候。还有,哎呀,那些地球人完全…安全地弄脏了你的线。
void getdir(char *dir)
{
    char *direct;
    char filepath[250];

    ...

    direct = malloc(strlen(dir) + strlen(name->d_name) + 2);
    strcpy(direct, dir);
    strcat(direct, "/");
    strcat(direct, name->d_name);

    getdir(direct);
    free(direct);
}
else
{
    // Concatenate file name
    strcpy(filepath, dir);
    strcat(filepath, "/");
    strcat(filepath, name->d_name);

    if(filepath[strlen(filepath) - 1] != '~')
        readFile(filepath);
}