C语言遍历文件,怎么子文件夹也被打印出来了?
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void string_replace(string & strBig, const string & strsrc, const string &strdst)
{
string::size_type pos=0;
string::size_type srclen=strsrc.size();
string::size_type dstlen=strdst.size();
while( (pos=strBig.find(strsrc, pos)) != string::npos)
{
strBig.replace(pos, srclen, strdst);
pos += dstlen;
}
}
void filesearch(string path)
{
string s;
///
struct _finddata_t filefind;
string curr=path+"\\*.*";
int done =0,handle;
if(!(handle=_findfirst(curr.c_str(),&filefind)))
{
return ;
}
while(!(done=_findnext(handle,&filefind)))
{
if(strcmp(filefind.name,"..")==0)
{
continue;
}
if(_A_SUBDIR==filefind.attrib)
{
curr=path+"\\"+filefind.name;
filesearch(curr);
continue;
}
s=curr;
string_replace(s,"*.*",filefind.name);
FILE *fp;
fp=fopen("1.txt","a+");
fprintf(fp,"%s\n",s.c_str());
fclose(fp);
}
_findclose(handle);
}
int main()
{
FILE *f;
f=fopen("1.txt","w");
fclose(f);
filesearch("f:");
system("start 1.txt");
return 0;
}
我想把指定文件夹下的所有文件找出来,但是打印的时候怎么有的子文件夹也被打印出来了?
------解决方案--------------------
可以是你判断文件夹的访问有问题
在 if(_A_SUBDIR==filefind.attrib) 这里下断点, 看是目录是这个条件是否进入就知道了
------解决方案--------------------
_A_SUBDIR==filefind.attrib
wrong!
_A_SUBDIR==(filefind.attrib & _A_SUBDIR)