社区
C语言
帖子详情
怎么对当前目录下文件夹内的文件遍历
fujingn188
2006-02-25 09:49:01
如题
...全文
208
7
打赏
收藏
怎么对当前目录下文件夹内的文件遍历
如题
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
7 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
ywchen2000
2006-02-26
打赏
举报
回复
unix/linux 下
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include <dirent.h>
void myfindfile(list<string> *needfile); //find file function
bool isfile(char* str)
{
long lens;
int i;
char *value,*newaddress;
lens=strlen(str);
for(i=lens;i>0;i--)
{
if(*(str+i)=='.')
{
value=(char *)malloc(4*sizeof(char));
newaddress=(str+i+1);
strcpy(value,newaddress);
if(strcmp(value,"map")==0)
{
free(value);
return true;
}
else
{
free(value);
return false;
}
}
}
return false;
}
char *fullpath;
void myfind(char* pathname,list<string> *xfilename)
{
int size;
size=pathconf("/",_PC_PATH_MAX);
size++;
fullpath=(char*)malloc(size);
strcpy(fullpath,pathname);
myfindfile(xfilename);
}
void myfindfile(list<string> *needfile) //find file function
{
DIR *sp;
struct dirent *dirp;
struct stat buf;
char *pathname,*newpathname,*ptr,*fullname,*tempstr;
long lens,newlen,fulllen,templen;
string newline;
ptr=fullpath+strlen(fullpath);
*ptr++='/';
*ptr=0;
sp=opendir(fullpath);
if(sp!=NULL)
{
while((dirp=readdir(sp))!=NULL)
{
strcpy(ptr,dirp->d_name);
if(lstat(fullpath,&buf)<0)
{
continue;
}
if(S_ISDIR(buf.st_mode))
{
if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)
{
continue;
}
myfindfile(needfile);
}
if(S_ISREG(buf.st_mode))
{
if(isfile(dirp->d_name)==true)
{
newline=fullpath;
// cout<<newline<<endl;
needfile->createlist(newline);
}
}
else
{
continue;
}
}
}
if(closedir(sp)<0)
{
printf("close dir failed");
}
}
sk_fault
2006-02-26
打赏
举报
回复
学习学习!!
Rick_ang
2006-02-25
打赏
举报
回复
我看了我们的课本..有一个项目是做一个Shell..其中就有要求类似DOS中输入Dir 显示当前目录的功能..正干瞪眼呢呵呵..我也很喜欢OS..
du51
2006-02-25
打赏
举报
回复
OS用不到.OS我特别喜欢.学得算是比较用功的.
主要是进程那一块.内存那一块.
Rick_ang
2006-02-25
打赏
举报
回复
好..学习了..这学期学OS估计能用到
du51
2006-02-25
打赏
举报
回复
#include<iostream>
#include<string>
#include<io.h>
using namespace std;
void filesearch(string path,int layer)
{
struct _finddata_t filefind;
string curr=path+"\\*.*";
int done=0,i,handle;
if((handle=_findfirst(curr.c_str(),&filefind))==-1)return;
while(!(done=_findnext(handle,&filefind)))
{
if(strcmp(filefind.name,".."))//不返回
{
if ((_A_SUBDIR==filefind.attrib))
{
for(i=0;i<layer;i++)cout<<" ";
cout<<filefind.name<<"(dir)"<<endl;
curr=path+"\\"+filefind.name;
filesearch(curr,layer+1);
}
else
{
for(i=0;i<layer;i++)cout<<" ";
cout<<filefind.name<<endl;
}
}
}
_findclose(handle);
}
int main()
{
string path;
cout<<"请输入目录"<<endl;
cin>>path;
filesearch(path,0);
system("PAUSE");
return 0;
}
llf_hust
2006-02-25
打赏
举报
回复
函数名: findfirst, findnext
功 能: 搜索磁盘目录; 取得下一个匹配的findfirst模式的文件
用 法: int findfirst(char *pathname, struct ffblk *ffblk, int attrib);
int findnext(struct ffblk *ffblk);
程序例:
/* findnext example */
#include <stdio.h>
#include <dir.h>
int main(void)
{
struct ffblk ffblk;
int done;
printf("Directory listing of *.*\n");
done = findfirst("*.*",&ffblk,0);
while (!done)
{
printf(" %s\n", ffblk.ff_name);
done = findnext(&ffblk);
}
return 0;
}
C语言
遍历
文件
夹
里所有
文件
该函数通过递归方式实现了对指定路径下的所有
文件
及子
文件
夹
的
遍历
。其核心代码如下: ```cpp if ((handle = _findfirst(curr.c_str(), &filefind)) == -1) { return; } while (!(done = _findnext(handle, &file...
VC++6.0
遍历
一个
文件
夹
下的所有目录
文件
VC++6.0
遍历
一个
文件
夹
下的所有目录
文件
已调通~
Matlab
文件
夹
的层次
遍历
和深度
遍历
编程
每次循环,我们从队列中取出一个
文件
夹
路径,打印该路径,并获取该
文件
夹
中的所有子
文件
夹
。...在上面的代码中,我们首先打印当前
文件
夹
的路径,然后获取当前
文件
夹
中的所有子
文件
夹
。函数来
遍历
每个子
文件
夹
。
BAT 一键
遍历
出
当前目录
及子
文件
夹
下指定类型的
文件
用户可以输入要提取的
文件
类型(如MP4、JPG等),然后脚本将在
当前目录
及其子
文件
夹
下搜索该类型的
文件
,并将结果保存到一个文本
文件
中(命名为"
遍历
结果_
文件
类型.txt")。最后,脚本会打开该文本
文件
以供查看。...
DOS命令(bat批处理脚本)
遍历
目录、
遍历
子目录下的
文件
、
遍历
数字和
遍历
文件
内容
DOS命令(bat批处理脚本)
遍历
目录、
遍历
子目录下的
文件
、
遍历
数字和
遍历
文件
内容
C语言
70,020
社区成员
243,263
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章