void LoadFile(const char *path)
{
DIR *pDir =0;
struct dirent *ent =0;
char childpath[512]={0};
pDir=opendir(path);
if(!pDir)
{
printf("opendir faile:%s\r\n",path);
return;
}
{
DIR *pDir =0;
struct dirent *ent =0;
char childpath[512]={0};
pDir=opendir(path);
if(!pDir)
{
printf("opendir faile:%s\r\n",path);
return;
}
while((ent=readdir(pDir))!=NULL)
{
if(ent->d_type & DT_DIR)
{
if(strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0)
continue;
memset(childpath,0,sizeof(childpath));
sprintf(childpath,"%s/%s",path,ent->d_name);
printf("dir:%s\r\n",childpath);
LoadFile(childpath);
}
else
{
char filename[256]={0};
sprintf(filename,"%s/%s",path,ent->d_name);
printf("file:%s\r\n",filename);
}
}
closedir(pDir);
}
本文介绍了一个用于递归地遍历文件夹并加载文件的C语言函数。该函数使用了POSIX目录流(DIR)操作来读取指定路径下的所有文件及子目录,并能够递归地进入每个子目录进行进一步的文件加载。文章通过具体代码展示了如何处理目录和文件,包括打开目录、读取目录条目、处理特殊目录如.和..以及关闭目录等步骤。
4990

被折叠的 条评论
为什么被折叠?



