c语言遍历指定文件夹目录树下所有文件

linux下 c语言递归遍历文件夹下所有文件和子文件夹(附上替换文本文件内容的方法)
linux下 c语言递归遍历文件夹下所有文件和子文件夹(附上替换文本文件内容的方法)
[摘要:#include stdio.h #include sys/dir.h #include string #include sys/stat.h //判别是不是为文件夹 bool isDir(const char* path); //遍历文件夹的驱动函数 void findFiles(const char *path); //遍历文件夹de递回函数 void findFil]
#include &stdio.h& #include &sys/dir.h& #include &string& #include &sys/stat.h&
//判断是否为文件夹 bool isDir(const char* path);
//遍历文件夹的驱动函数 void findFiles(const char *path);
//遍历文件夹de递归函数 void findFiles(const char *path, int recursive);
#define MAX_LEN 1024 * 512
int main(int argc, const char * argv[]) {
findFiles(&未命名文件夹&);
return 0; }
//判断是否为目录 bool isDir(const char* path) {
lstat(path, &st);
return 0 != S_ISDIR(st.st_mode); }
//遍历文件夹的驱动函数 void findFiles(const char *path) {
char temp[MAX_LEN];
//去掉末尾的'/'
len = strlen(path);
strcpy(temp, path);
if(temp[len - 1] == '/') temp[len -1] = '';
if(isDir(temp))
//处理目录
int recursive = 1;
findFiles(temp, recursive);
//输出文件
printf(&======%sn&, path);
//遍历文件夹de递归函数 void findFiles(const char *path, int recursive) {
struct dirent *
char temp[MAX_LEN];
pdir = opendir(path);
while((pdirent = readdir(pdir)))
//跳过&.&和&..&
if(strcmp(pdirent-&d_name, &.&) == 0
|| strcmp(pdirent-&d_name, &..&) == 0)
sprintf(temp, &%s/%s&, path, pdirent-&d_name);
//当temp为目录并且recursive为1的时候递归处理子目录
if(isDir(temp) && recursive)
findFiles(temp, recursive);
printf(&======%sn&, temp);
printf(&opendir error:%sn&, path);
closedir(pdir); }
附上一个C语言实现文本替换的例子
#include&cstdlib& #include&string& #include&cstring& #include&fstream& void myreplace(const string& filename,const string& tofind,const string& toreplace) {
ifstream fin(filename.c_str(),ios_base::binary);
string str(,0);
fin.read(&str[0],2*);
fin.close();
ofstream fout(filename.c_str(),ios_base::binary);
string::size_type beg=0,pos,find_size=tofind.size(),replace_size=toreplace.size();
while((pos=str.find(tofind,beg))!=string::npos)
fout.write(&str[beg],pos-beg);
fout.write(&toreplace[0],replace_size);
beg=pos+find_
fout.write(&str[beg],strlen(str.c_str())-beg);
fout.close(); } int main() {
myreplace(&abca.txt&,&123ABCD123&,&456EFG456&); }
感谢关注 Ithao123C/c++频道,是专门为互联网人打造的学习交流平台,全面满足互联网人工作与学习需求,更多互联网资讯尽在 IThao123!
Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。
Hadoop是一个由Apache基金会所开发的分布式系统基础架构。
用户可以在不了解分布式底层细节的情况下,开发分布式程序。充分利用集群的威力进行高速运算和存储。
Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上;而且它提供高吞吐量(high throughput)来访问应用程序的数据,适合那些有着超大数据集(large data set)的应用程序。HDFS放宽了(relax)POSIX的要求,可以以流的形式访问(streaming access)文件系统中的数据。
Hadoop的框架最核心的设计就是:HDFS和MapReduce。HDFS为海量的数据提供了存储,则MapReduce为海量的数据提供了计算。
产品设计是互联网产品经理的核心能力,一个好的产品经理一定在产品设计方面有扎实的功底,本专题将从互联网产品设计的几个方面谈谈产品设计
随着国内互联网的发展,产品经理岗位需求大幅增加,在国内,从事产品工作的大部分岗位为产品经理,其实现实中,很多从事产品工作的岗位是不能称为产品经理,主要原因是对产品经理的职责不明确,那产品经理的职责有哪些,本专题将详细介绍产品经理的主要职责
IThao123周刊用c语言,然后我遍历的结果是目录下的文件夹。。所以,求大神回答下这个问题啊。
FindFirstFile, FindNextFile, FindClose
已有帐号?
无法登录?
社交帐号登录
字体、NSIS、C/C++ 爱好者赖半仙原创:Linux下C语言遍历目录内文件很简单的一个函数
赖半仙原创:Linux下C语言遍历目录内文件很简单的一个函数
转载请注明文章地址,尊重作者赖半仙的劳动成果,谢谢支持:&&& 网上关于linux下遍历一个目录的文件代码有很多,比如和 这两个例子中所说都用到了S_ISDIR()这个宏定义判断是否是目录,自己写的具体代码如下:
//遍历路径,缩进数量控制void List( char *path, int indent ){&&& struct&&& dirent* ent = NULL;&&& DIR&&& &&& *pD&&& char&&& dir[512];&&& struct stat&&&&& && && &&& if( (pDir=opendir(path))==NULL )&&& {&&& &&& fprintf( stderr, "Cannot open directory:%s\n", path );&&& &&&&&& }&&& &&& while( (ent=readdir(pDir))!=NULL )&&& {&&& &&& //得到读取文件的绝对路径名&&& snprintf( dir, 512,"%s/%s", path, ent-&d_name );&&& &&& //得到文件信息&&& lstat( dir, &statbuf);&&& //判断是目录还是文件&&& if( S_ISDIR(statbuf.st_mode) )&&& {&&&&&&&&&&& //排除当前目录和上级目录&&&&&&&&&&& if(strcmp( ".",ent-&d_name) == 0 || strcmp( "..",ent-&d_name) == 0)&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&& }&&& &&&&&&&&&&& //如果是子目录,递归调用函数本身,实现子目录中文件遍历 &&&&&&&&&&& printf( "%*s子目录:%s/\n", indent, "", ent-&d_name );&&&&&&&&&&& //递归调用,遍历子目录中文件&&&&&&&&&&& List( dir, indent+4 );&&&&&&&&&&& &&& &&& }&&& else&&& {&&& &&& printf( "%*s文件:%s\n", indent, "", ent-&d_name );&&& }&&&&&&& &&& }&&& closedir(pDir);}int main(int argc, char* argv[]){&&& if(argc == 2)&&& {&&&&&&& List( argv[1], 2 );&&& }&&& else&&& {&&&&&&& List( ".", 2);&&& }&&& return 0;}linux程序设计第三版书上的示例代码如下:
#include &unistd.h&#include &stdio.h&#include &dirent.h&#include &string.h&#include &sys/stat.h&void printdir(char *dir, int depth){&&& DIR *&&& struct dirent *&&&&&& if((dp = opendir(dir)) == NULL) {&&&&&&& fprintf(stderr,"cannot open directory: %s\n", dir);&&&&&&&&&& }&&& 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)&&&&&&&&&&&&&&&&&&&&&&&&&& 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=&&& else&&&&&&& topdir=argv[1];&&& printf("Directory scan of %s\n",topdir);&&& printdir(topdir,0);&&& printf("done.\n");&&& exit(0);}
发表评论:
馆藏&34671
TA的推荐TA的最新馆藏[转]&怎么用c语言实现遍历某目录或文件夹里的所有文件(所有类型的文件)?
用c语言,然后我遍历的结果是目录下的文件夹。。所以,求大神回答下这个问题啊。
FindFirstFile, FindNextFile, FindClose
已有帐号?
无法登录?
社交帐号登录相关热词搜索:}

我要回帖

更多关于 c语言遍历文件夹 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信