Linux下遍历文件夹内所有文件并将需要文件地址添加到链表中:
闲话少序,直接上代码
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include<iostream>
using namespace std;
/**************************************************************************************************
* Function Name : list_dir_name *
* Description : 给列表内添加一个目录的文件 *
* Date : 2011-7-12 *
* Parameter : 当前链表头节点HeadPL,添加的文件夹路径dirname *
* Return Code : 返回标志,0代表添加成功,1表示添加失败 *
* Author : xxmain *
**************************************************************************************************/
bool list_dir_name(char* dirname,PList *HeadPL)
{
DIR* dp;
struct dirent* dirp;
struct stat st;
PList *p;
PList *q;
p = HeadPL;
/* open dirent directory */
if((dp = opendir(dirname)) == NULL)
{
perror("opendir");
return false;
}
/*** read all files in this dir**/
while((dirp = readdir(dp)) != NULL)
{
char fullname[255];
memset(fullname, 0, sizeof(fullname));
/* ignore hidden files */
length=strlen(dirp->d_name);
if((dirp->d_name[0] == '.')||(dirp->d_name[length-1] !='3')||(dirp->d_name[length-2] !='p')||(dirp->d_name[length-3] !='m')||(dirp->d_name[length-4] !='.'))
{
continue; }strncpy(fullname, dirname, sizeof(fullname)); strncat(fullname, "/", sizeof("/")); strncat(fullname, dirp->d_name, sizeof(dirp->d_name));string fname = string(fullname);
/* 遍历链表到最后的节点 */ while(p != NULL)
{
q = p;
p = p->next;
}
/* 新建节点*/
PList *tmp = (PList*)new(PList);
tmp->FileUrl = fname;
tmp->next = NULL;
if(HeadPL == NULL)
{
HeadPL =tmp;
q = p = HeadPL;
}
else
{
p = tmp;
q->next =p;
}
}
return true;}
链表那快的结构就不发了,很简单的,大家自己看看吧~呵呵~