首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置链接列表以存储组ID和文件路径的正确方法

设置链接列表以存储组ID和文件路径的正确方法
EN

Stack Overflow用户
提问于 2021-02-26 07:33:11
回答 1查看 66关注 0票数 0

我正在尝试创建一个链接列表数据结构,它将允许我存储Group #以及它所属的文件目录的文件路径。该程序打开当前目录并从当前目录获取所有常规文件,它输出每个文件的路径,并尝试将路径插入链接列表中。对于它插入的每个文件路径,将创建一个新的groupID (将1添加到以前的groupID #),从第一个groupID的1开始。到目前为止,这是我的代码:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>

typedef struct FileGroups
{
    int groupID;
    char *path;
    struct FileGroups* next;
} FileGroups;

FileGroups *head;

void insert(char *path)
{
    FileGroups *temp;
    temp = (FileGroups*)malloc(sizeof(FileGroups));
    temp->groupID += 1;
    temp->path = path;
    temp->next = head;
    head = temp;
    temp = temp ->next;
}

void print()
{
    FileGroups *temp;
    temp = head;
    printf("\nLinked list: \n");
    while(temp!=NULL)
    {
        printf("%d %s\n", temp->groupID, temp->path);
        temp = temp->next;
    }
}

void listFilesRecursively(const char *basePath)
{
    char path[1024];
    struct dirent *dp;
    DIR *dir = opendir(basePath);

    if (!dir)
    {
        return;
    }

    while ((dp = readdir(dir)) != NULL)
    {
        if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
        {
            struct stat sb;

            strcpy(path, basePath);
            strcat(path, "/");
            strcat(path, dp->d_name);

            if(stat(path, &sb) == 0 && S_ISREG(sb.st_mode))
            {
                printf("%s\n", path);
                insert(path);
            }

            else
            {
                return;
            }
        }
    }
    closedir(dir);
}

int main()
{
    listFilesRecursively(".");

    print();

    return 0;
}

当我打印链接列表时,输出如下:

在上半部分,您可以看到我当前目录和下面的所有常规文件都是我的链接列表,它似乎只存储在我当前目录中列出的最后一个文件,而在左边,我们看到groupID #也没有改变,而不是添加了1,每个文件路径都添加了1,它停留在groupID #1。任何关于我出错的地方的建议或指示都是非常感谢的。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-26 08:31:45

每次迭代时,path都会被覆盖并分配给列表的新元素,但是您不会将字符串复制到新的缓冲区中。

因此,列表的每个元素都指向在path中声明的listFilesRecursively(),其中只包含最后列出的文件

代码语言:javascript
复制
...
int GroupID = 1;

void listFilesRecursively(const char *basePath)
    ...
    char path[1024];
    ...
// Here path is overwritten
    strcpy(path, basePath);
    strcat(path, "/");
    strcat(path, dp->d_name);
    ...
    insert(path);

void insert(char *path)
    ...
// This is copying only the pointer to path
    temp->path = path;

插入时应该为路径分配一个新的缓冲区。

代码语言:javascript
复制
void insert(char *path)
{
    FileGroups *temp;
    temp = (FileGroups*)malloc(sizeof(FileGroups));
/**
 * This should not be += 1
 * Memory allocated by malloc is not initialised.
 * Value at these locations are indeterminate.
 *
 * To know the next groupID I'm using a simple
 * global variable as suggested by Serge Ballesta
 * in his comment
 */
    temp->groupID = GroupID++;
//------------------------------------------------------
    temp->path = malloc(strlen(path)*sizeof(char));
    strcpy(temp->path, path);
//------------------------------------------------------
    temp->next = head;
    head = temp;
    temp = temp ->next;
}

您可以在这里运行它,https://onlinegdb.com/O6cTm7Hu1

关于malloc() Is malloc() initializing allocated array to zero?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66381783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档