C 设置链接列表以存储组ID和文件路径的正确方法
我正在尝试创建一个链表数据结构,它允许我存储组ID以及文件所属目录的文件路径。该程序打开当前目录并从当前目录获取所有常规文件,它输出每个文件的路径,并尝试将路径插入链接列表。对于插入的每个文件路径,将创建一个新的groupID(将1添加到以前的groupID上),第一个groupID从1开始。以下是迄今为止我的代码:C 设置链接列表以存储组ID和文件路径的正确方法,c,unix,linked-list,system,singly-linked-list,C,Unix,Linked List,System,Singly Linked List,我正在尝试创建一个链表数据结构,它允许我存储组ID以及文件所属目录的文件路径。该程序打开当前目录并从当前目录获取所有常规文件,它输出每个文件的路径,并尝试将路径插入链接列表。对于插入的每个文件路径,将创建一个新的groupID(将1添加到以前的groupID上),第一个groupID从1开始。以下是迄今为止我的代码: #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include &
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
typedef结构文件组
{
int-groupID;
字符*路径;
结构文件组*下一步;
}文件组;
文件组*头;
无效插入(字符*路径)
{
文件组*temp;
temp=(文件组*)malloc(sizeof(文件组));
temp->groupID+=1;
温度->路径=路径;
温度->下一步=头部;
压头=温度;
温度=温度->下一步;
}
作废打印()
{
文件组*temp;
温度=水头;
printf(“\n链接列表:\n”);
while(temp!=NULL)
{
printf(“%d%s\n”,temp->groupID,temp->path);
温度=温度->下一步;
}
}
递归作废列表文件(const char*basePath)
{
字符路径[1024];
结构方向*dp;
DIR*DIR=opendir(基本路径);
如果(!dir)
{
返回;
}
而((dp=readdir(dir))!=NULL)
{
如果(strcmp(dp->d_name,“.”)=0和&strcmp(dp->d_name,“…”)!=0)
{
结构统计某人;
strcpy(路径、基本路径);
strcat(路径“/”;
strcat(路径,dp->d_名称);
if(stat(path,&sb)==0和&S_ISREG(sb.st_模式))
{
printf(“%s\n”,路径);
插入(路径);
}
其他的
{
返回;
}
}
}
closedir(dir);
}
int main()
{
列表文件以递归方式(“.”)显示;
打印();
返回0;
}
当我打印出链表时,我得到以下输出:
在上半部分,您可以看到我当前目录中的所有常规文件,下面是我的链接列表,它似乎只存储我当前目录中列出的最后一个文件,在左侧,我们还可以看到groupID#也没有改变,而不是在添加每个文件路径时添加1,它被固定在groupID#1。如果您对我的错误有任何建议或指点,我们将不胜感激。谢谢 每次迭代时,
路径将被覆盖并分配给列表的新元素,但您没有将字符串复制到新缓冲区。
因此,在列表的末尾,每个元素都将指向在列表文件递归()中声明的路径
,该路径将只包含列出的最后一个文件
...
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;
插入时应为路径分配新缓冲区
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;
}
你可以在这里运行它
关于malloc()
OP希望在每个元素上增加groupID
。由于head
已经是一个全局变量,您可以使用全局intgroupid=1然后在函数中temp->groupID=groupID++代码>。或者,您可以使用静态int-GroupID=1代码>在函数本身中。@SergeBallesta你是对的,我在编辑答案时忘记了这一点。谢谢