您的位置:首页 > 编程语言 > C语言/C++

如何使用C语言搜索指定目录下的所有文件?

2013-03-19 11:26 597 查看
#include <stdio.h>

#include <dirent.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;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐