导航

DEV C++, 利用链表实现目录内所有文件列表显示

Posted on 2020-11-05 19:11  sealong  阅读(347)  评论(0)    收藏  举报

include <stdio.h>

include <dirent.h>

include <string.h>

void main(int argc,char *argv[])
{
DIR *directory_pointer;
struct dirent *entry;
char path[80];
struct FileList
{
char filename[64];
struct FileList next;
}start,
node;
puts("Input the directory:");
gets(path);
if ((directory_pointer=opendir(path))==NULL)
printf("Error opening %s\n",path);
else
{
start.next=NULL;
node=&start;
while ((entry=readdir(directory_pointer))!=NULL)
{
node->next=(struct FileList *)malloc(sizeof(struct FileList));
node=node->next;
strcpy(node->filename,entry->d_name);
node->next=NULL;
}
closedir(directory_pointer);
node=start.next;
while(node)
{
printf("%s\n",node->filename);
node=node->next;
}
}
}