怎么对当前目录下文件夹内的文件遍历

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;
}

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧