利用链表实现目录内所有文件列表显示
2008-03-08 12:47:04 来源:WEB开发网核心提示:#include <stdio.h>#include <dirent.h>#include <alloc.h>#include <string.h>void main(int argc,char *argv[]){ DIR *Directory_pointer; st
#include <stdio.h>
#include <dirent.h>
#include <alloc.h>
#include <string.h> void main(int argc,char *argv[])
{
DIR *Directory_pointer;
strUCt dirent *entry;
struct FileList
{
char filename[64];
struct FileList *next;
}start,*node;
if (argc!=2)
{
PRintf("Must specify a directory\n");
exit(1);
}
if ((directory_pointer=opendir(argv[1]))==NULL)
printf("Error opening %s\n",argv[1]);
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;
}
}
}
#include <dirent.h>
#include <alloc.h>
#include <string.h> void main(int argc,char *argv[])
{
DIR *Directory_pointer;
strUCt dirent *entry;
struct FileList
{
char filename[64];
struct FileList *next;
}start,*node;
if (argc!=2)
{
PRintf("Must specify a directory\n");
exit(1);
}
if ((directory_pointer=opendir(argv[1]))==NULL)
printf("Error opening %s\n",argv[1]);
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;
}
}
}
更多精彩
赞助商链接