当前位置: 编程技术>c/c++/嵌入式
深入探讨:linux中遍历文件夹下的所有文件
来源: 互联网 发布时间:2014-10-17
本文导语: linux C 遍历目录及其子目录 代码如下:#include #include #include #include #include #include #include using namespace std;void listDir(char *path) { DIR *pDir ; struct dirent *ent ; in...
linux C 遍历目录及其子目录
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void listDir(char *path)
{
DIR *pDir ;
struct dirent *ent ;
int i=0 ;
char childpath[512];
pDir=opendir(path);
memset(childpath,0,sizeof(childpath));
while((ent=readdir(pDir))!=NULL)
{
if(ent->d_type & DT_DIR)
{
if(strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0)
continue;
sprintf(childpath,"%s/%s",path,ent->d_name);
printf("path:%s/n",childpath);
listDir(childpath);
}
else
{
coutd_name, &sb) >= 0 && S_ISDIR(sb.st_mode) && depth d_name, depth + 1);
}
}
closedir(d);
return 0;
}
int main()
{
int depth = 1;
int i;
trave_dir("/usr/keygoe/ini/", depth);
for(i = 0; i < len; i++)
{
printf("%st", filename[i]);
}
printf("n");
return 0;
}
Linux下C语言遍历文件夹
学习了LINUX下用C语言遍历文件夹,一些心得
struct dirent中的几个成员:
d_type:4表示为目录,8表示为文件
d_reclen:16表示子目录或文件,24表示非子目录
经过本人亲自试验发现:d_reclen:16表示子目录或以.开头的隐藏文件,24表示普通文本文件,28为二进制文件,等等
d_name:目录或文件的名称
具体代码如下,仅供参考
#include
#include
#include
void List(char *path)
{
struct dirent* ent = NULL;
DIR *pDir;
pDir=opendir(path);
while (NULL != (ent=readdir(pDir)))
{
if (ent->d_reclen==24)
{
if (ent->d_type==8)
{
printf("普通文件:%sn", ent->d_name);
}
else
{
printf("子目录:%sn",ent->d_name);
List(ent->d_name);
printf("返回%sn",ent->d_name);
}
}
}
}
int main(int argc, char *argv[])
{
List(argv[1]);
return 0;
}
上面函数修改后:
void List(char *path)
{
printf("路径为[%s]n", path);
struct dirent* ent = NULL;
DIR *pDir;
pDir=opendir(path);
//d_reclen:16表示子目录或以.开头的隐藏文件,24表示普通文本文件,28为二进制文件,还有其他……
while (NULL != (ent=readdir(pDir)))
{
printf("reclen=%d type=%dt", ent->d_reclen, ent->d_type);
if (ent->d_reclen==24)
{
//d_type:4表示为目录,8表示为文件
if (ent->d_type==8)
{
printf("普通文件[%s]n", ent->d_name);
}
}
else if(ent->d_reclen==16)
{
printf("[.]开头的子目录或隐藏文件[%s]n",ent->d_name);
}
else
{
printf("其他文件[%s]n", ent->d_name);
}
}
}
#include
#include
#include
#include
void dir_scan(char *path, char *file);
int count = 0;
int main(int argc, char *argv[])
{
struct stat s;
if(argc != 2){
printf("one direction requriedn");
exit(1);
}
if(lstat(argv[1], &s) d_name,"..")==0) continue;
if(stat(pdirent->d_name,&f_ftime)!=0) return -1 ;
if(S_ISDIR(f_ftime.st_mode)) continue; /*子目录跳过*/
fcnt++;
printf("文件:%sn",pdirent->d_name);
}
printf("文件总数%dn",fcnt);
closedir(pdir);
return 0;
}
#include
#include
#include
#include
#include
void printdir(char *dir, int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr, "cannot open directory: %sn ", dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry-> d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)) {
/**//* Found a directory, but ignore . and .. */
if(strcmp( ". ",entry-> d_name) == 0 ||
strcmp( ".. ",entry-> d_name) == 0)
continue;
printf( "%*s%s/n ",depth, " ",entry-> d_name);
/**//* Recurse at a new indent level */
printdir(entry-> d_name,depth+4);
}
else printf( "%*s%sn ",depth, " ",entry-> d_name);
}
chdir( ".. ");
closedir(dp);
}
/**//* Now we move onto the main function. */
int main(int argc, char* argv[])
{
char *topdir, pwd[2]= ". ";
if (argc != 2)
topdir=pwd;
else
topdir=argv[1];
printf( "Directory scan of %sn ",topdir);
printdir(topdir,0);
printf( "done.n ");
exit(0);
}
代码如下:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void listDir(char *path)
{
DIR *pDir ;
struct dirent *ent ;
int i=0 ;
char childpath[512];
pDir=opendir(path);
memset(childpath,0,sizeof(childpath));
while((ent=readdir(pDir))!=NULL)
{
if(ent->d_type & DT_DIR)
{
if(strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0)
continue;
sprintf(childpath,"%s/%s",path,ent->d_name);
printf("path:%s/n",childpath);
listDir(childpath);
}
else
{
coutd_name, &sb) >= 0 && S_ISDIR(sb.st_mode) && depth d_name, depth + 1);
}
}
closedir(d);
return 0;
}
int main()
{
int depth = 1;
int i;
trave_dir("/usr/keygoe/ini/", depth);
for(i = 0; i < len; i++)
{
printf("%st", filename[i]);
}
printf("n");
return 0;
}
Linux下C语言遍历文件夹
学习了LINUX下用C语言遍历文件夹,一些心得
struct dirent中的几个成员:
d_type:4表示为目录,8表示为文件
d_reclen:16表示子目录或文件,24表示非子目录
经过本人亲自试验发现:d_reclen:16表示子目录或以.开头的隐藏文件,24表示普通文本文件,28为二进制文件,等等
d_name:目录或文件的名称
具体代码如下,仅供参考
代码如下:
#include
#include
#include
void List(char *path)
{
struct dirent* ent = NULL;
DIR *pDir;
pDir=opendir(path);
while (NULL != (ent=readdir(pDir)))
{
if (ent->d_reclen==24)
{
if (ent->d_type==8)
{
printf("普通文件:%sn", ent->d_name);
}
else
{
printf("子目录:%sn",ent->d_name);
List(ent->d_name);
printf("返回%sn",ent->d_name);
}
}
}
}
int main(int argc, char *argv[])
{
List(argv[1]);
return 0;
}
上面函数修改后:
代码如下:
void List(char *path)
{
printf("路径为[%s]n", path);
struct dirent* ent = NULL;
DIR *pDir;
pDir=opendir(path);
//d_reclen:16表示子目录或以.开头的隐藏文件,24表示普通文本文件,28为二进制文件,还有其他……
while (NULL != (ent=readdir(pDir)))
{
printf("reclen=%d type=%dt", ent->d_reclen, ent->d_type);
if (ent->d_reclen==24)
{
//d_type:4表示为目录,8表示为文件
if (ent->d_type==8)
{
printf("普通文件[%s]n", ent->d_name);
}
}
else if(ent->d_reclen==16)
{
printf("[.]开头的子目录或隐藏文件[%s]n",ent->d_name);
}
else
{
printf("其他文件[%s]n", ent->d_name);
}
}
}
代码如下:
#include
#include
#include
#include
void dir_scan(char *path, char *file);
int count = 0;
int main(int argc, char *argv[])
{
struct stat s;
if(argc != 2){
printf("one direction requriedn");
exit(1);
}
if(lstat(argv[1], &s) d_name,"..")==0) continue;
if(stat(pdirent->d_name,&f_ftime)!=0) return -1 ;
if(S_ISDIR(f_ftime.st_mode)) continue; /*子目录跳过*/
fcnt++;
printf("文件:%sn",pdirent->d_name);
}
printf("文件总数%dn",fcnt);
closedir(pdir);
return 0;
}
#include
#include
#include
#include
#include
void printdir(char *dir, int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr, "cannot open directory: %sn ", dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry-> d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)) {
/**//* Found a directory, but ignore . and .. */
if(strcmp( ". ",entry-> d_name) == 0 ||
strcmp( ".. ",entry-> d_name) == 0)
continue;
printf( "%*s%s/n ",depth, " ",entry-> d_name);
/**//* Recurse at a new indent level */
printdir(entry-> d_name,depth+4);
}
else printf( "%*s%sn ",depth, " ",entry-> d_name);
}
chdir( ".. ");
closedir(dp);
}
/**//* Now we move onto the main function. */
int main(int argc, char* argv[])
{
char *topdir, pwd[2]= ". ";
if (argc != 2)
topdir=pwd;
else
topdir=argv[1];
printf( "Directory scan of %sn ",topdir);
printdir(topdir,0);
printf( "done.n ");
exit(0);
}