转自:http://blog.csdn.net/wallwind/article/details/7528474
linux C 遍历目录及其子目录
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
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
{
cout<<ent->d_name<<endl;
}
}
}
int main(int argc,char *argv[])
{
listDir(argv[1]);
return 0;
}
//////////////////////////////////////////////////////////
Linux C :遍历输出指定目录下的所有文件
在Linux下opendir()、readdir()和closedir()这三个函数主要用来遍历目录。在使用这三个函数前必须先包括以下两个头文件:
#include <sys/types.h>
#include <dirent.h>
opendir函数的原型为:
DIR *opendir(const char *name);
它返回一个DIR*类型,这就是一个句柄啦,你不用管它的内部结构是什么样的,只要知道这个句柄就是等一下要传给readdir()函数的参数就行了。
readdir函数的原型为:
struct dirent *readdir(DIR *dir);
看它的参数就知道该参数是opendir函数返回的句柄,而该函数的返回值是struct dirent*
类型,这里我们必须了解一下这个结构体:
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file */
char d_name[256]; /* filename */
};
这个结构体的d_name存放的就是文件的名字,这里的文件包括普通文件,目录文件等等,在linux的思想中,所有的东西都是文件。
closedir函数的原型为:
int closedir(DIR *dir);
这个函数就不用多说了,一般有开(open),就有关(close),这样的结构经常可出看到,如fopen,fclose等等。
三个函数介绍完了,直接来一个例子吧:
**********************************************SearchDir.c****************************************************
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
char filename[256][256];
int len = 0;
int trave_dir(const
char* path, int depth)
{
DIR *d; //声明一个句柄
struct dirent *file; //readdir函数的返回值就存放在这个结构体中
struct stat sb;
if(!(d = opendir(path)))
{
printf("error opendir %s!!!\n",path);
return -1;
}
while((file = readdir(d)) != NULL)
{
//把当前目录.,上一级目录..及隐藏文件都去掉,避免死循环遍历目录
if(strncmp(file->d_name, ".", 1) == 0)
continue;
strcpy(filename[len++], file->d_name); //保存遍历到的文件名
//判断该文件是否是目录,及是否已搜索了三层,这里我定义只搜索了三层目录,太深就不搜了,省得搜出太多文件
if(stat(file->d_name, &sb) >= 0 && S_ISDIR(sb.st_mode)&& depth <= 3)
{
trave_dir(file->d_name, depth + 1);
}
}
closedir(d);
return 0;
}
int main()
{
int depth = 1;
int i;
string str="/usr/keygoe/ini/";
trave_dir(str.c_str(),depth);
for(i = 0; i < len; i++)
{
printf("%s\t", 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 <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
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("普通文件:%s\n", ent->d_name);
}
else
{
printf("子目录:%s\n",ent->d_name);
List(ent->d_name);
printf("返回%s\n",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=%d\t", 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);
}
}
}
转CU,地址:
http://blog.chinaunix.net/u/29024/showart_484896.html
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
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 requried\n");
exit(1);
}
if(lstat(argv[1], &s) < 0){
printf("lstat error\n");
exit(2);
}
//判断一个路径是否是目录
if(!S_ISDIR(s.st_mode)){
printf("%s is not a direction name\n", argv[1]);
exit(3);
}
dir_scan("", argv[1]);
printf("total: %d files\n", count);
exit(0);
}
void dir_scan(char *path, char *file)
{
struct stat s;
DIR *dir;
struct dirent *dt;
char dirname[50];
memset(dirname, 0, 50*sizeof(char));
strcpy(dirname, path);
if(lstat(file, &s) < 0){
printf("lstat error\n");
}
if(S_ISDIR(s.st_mode)){
strcpy(dirname+strlen(dirname), file);
strcpy(dirname+strlen(dirname), "/");
if((dir = opendir(file)) == NULL){
printf("opendir %s/%s error\n");
exit(4);
}
if(chdir(file) < 0) {
printf("chdir error\n");
exit(5);
}
while((dt = readdir(dir)) != NULL){
if(dt->d_name[0] == '.'){
continue;
}
dir_scan(dirname, dt->d_name);
}
if(chdir("..") < 0){
printf("chdir error\n");
exit(6);
}
}else{
printf("%s%s\n", dirname, file);
count++;
}
}
linux c 下如何获得目录下的文件数目。
int main(int argc, char **argv)
{
DIR * pdir;
struct dirent * pdirent;
struct stat f_ftime;
int fcnt;/*文件数目统计*/
pdir=opendir("./");
if(pdir==NULL)
{ return(-1); }
fcnt=0;
for(pdirent=readdir(pdir);pdirent!=NULL;pdirent=readdir(pdir))
{
if(strcmp(pdirent->d_name,".")==0||strcmp(pdirent->d_name,"..")==0)continue;
if(stat(pdirent->d_name,&f_ftime)!=0) return -1 ;
if(S_ISDIR(f_ftime.st_mode)) continue; /*子目录跳过*/
fcnt++;
printf("文件:%s\n",pdirent->d_name);
}
printf("文件总数%d\n",fcnt);
closedir(pdir);
return 0;
}
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
void printdir(char *dir, int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr, "cannot open directory: %s\n ", 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%s\n ",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 %s\n ",topdir);
printdir(topdir,0);
printf( "done.\n ");
exit(0);
}
分享到:
相关推荐
linux c 实现遍历指定路径下的文件夹及文件 ,修改了一下程序,使其可以在当前路径下以文件名“遍历结果.txt”输出运行结果; 运行结果改为树形输出方式。
Linux 刚刚开始学,挺实用的。Linux-C++-遍历指定文件夹路径下的文件列表并将路径写到txt文件。 如果没有积分,请私信我留下邮箱,我会尽快发过去
对linux命令tree的简单实现,遍历指定文件夹下的所有文件
本篇文章是对linux中遍历文件夹下的所有文件进行了详细的分析介绍,需要的朋友参考下
如何遍历文件夹查找文件如何遍历文件夹查找文件
遍历文件夹下的所有文件,测试用,伪代码,在linux环境下用c实现。
在linux 下的用c实现的遍历指定文件夹下的所有文件,并读出文件中的内容
C++遍历指定文件夹中的所有文件 遍历方法
C#实现,遍历ftp上的文件夹,文件名
linux及win跟目录递归遍历文件和文件夹及子文件夹 自己用的,
在Linux平台下实现的文件夹遍历类。采用了stack 和queue来实现循环深度优先遍历和循环广度优先遍历。采用pthread编程。类比于Windows平台的线程编程。压缩包中的文件MediaFileMgr.h可以扩展,具体扩展可根据使用者的...
在linux环境下通过zlib库压缩文件夹/目录成.zip文件的c++程序。测试ok、不乱码,如果想自己操作一边,请看我写的readme文档(包含说明和操作步骤),可以快速实现压缩。
发现php的文件夹函数好像没有提供遍历文件夹下的所有文件(包括子目录中的文件),于是,就想自己实现一个。 在写的时候发现一些操作文件夹的函数并不是自己想的那样。比如,dirname()根据提供的完整文件路径来取得...
主要 是通过学习线程池与互斥锁条件变量来线程之间的通信,输入目标目录或者是文件,用DIR函数,判断是目录还是普通文件,大文件还是小文件,大文件用LSEEK分段保存在链表中 ,分段复制。该小项目主要包含,线程池与...
简单的小例子,输入文件路径后可对路径进行遍历并导出到XML,生成Log文件。对遍历文件或导出XML不是很理解的话可以参考。只是闲暇时写的,并没有任何校验。内含注释。
这是一个用于获取指定目录下所有文件的信息的C++代码, 是用于linux系统的
测序的项目,每一个样本的数据都存放在各自的名称文件夹下,我需要下载一批数据的variant文件(.vcf),但是如果一个一个找,太麻烦了,会眼酸手酸,想偷一下懒;肯定不能改动文件的名字,就是拷贝到一个文件中,...
主要介绍了基于linux命令提取文件夹内特定文件路径,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
os.listdir:列出目录下的文件和文件夹os.path.join:拼接得到一个文件/文件夹的全路径os.path.isfile:判断是否是文件os.path.splitext:从名称中取出一个子部分 下面是目录操作的代码 复制代码 代码如下:def search...