这两个函数均在io.h里面。
首先了解一下一个文件结构体:
struct _finddata_t {
unsigned
attrib;
time_t
time_create;
time_t
time_access;
time_t
time_write;
_fsize_t
size;
char
name[260];
};
time_t,其实就是long
而_fsize_t,就是unsigned long
现在来解释一下结构体的数据成员吧。
attrib,就是所查找文件的属性:_A_ARCH(存档)、_A_HIDDEN(隐藏)、_A_NORMAL(正常)、_A_RDONLY(只读)、_A_SUBDIR(文件夹)、_A_SYSTEM(系统)。
time_create、time_access和time_write分别是创建文件的时间、最后一次访问文件的时间和文件最后被修改的时间。
size:文件大小
name:文件名。
再来看一下_findfirst函数:long _findfirst(const char *, struct _finddata_t*);
第一个参数为文件名,可以用"*.*"来查找所有文件,也可以用"*.cpp"来查找.cpp文件。第二个参数是_finddata_t结构体指针。若查找成功,返回文件句柄,若失败,返回-1。
然后,_findnext函数:int _findnext(long, struct _finddata_t *);
第一个参数为文件句柄,第二个参数同样为_finddata_t结构体指针。若查找成功,返回0,失败返回-1。
最后:_findclose()函数:int _findclose(long);
只有一个参数,文件句柄。若关闭成功返回0,失败返回-1。
代码一、
#include<iostream>
#include<io.h>
using namespace std;
int main()
{
_finddata_tfile;
longlf;
if((lf =_findfirst("D:\\files\\*.*",&file))==-1l)//_findfirst返回的是long型; long __cdecl_findfirst(const char *, struct _finddata_t *)
cout<<"文件没有找到!\n";
else
{
cout<<"\n文件列表:\n";
while(
_findnext( lf, &file ) ==0 )//int __cdecl _findnext(long, struct _finddata_t*);如果找到下个文件的名字成功的话就返回0,否则返回-1
{
cout<<file.name;
if(file.attrib ==_A_NORMAL)cout<<"
普通文件
";
else if(file.attrib ==_A_RDONLY)cout<<"
只读文件
";
else if(file.attrib == _A_HIDDEN)cout<<"
隐藏文件
";
elseif(file.attrib == _A_SYSTEM)cout<<"
系统文件
";
else if(file.attrib ==_A_SUBDIR)cout<<"
子目录
";
elseif(file.attrib ==_A_ARCH)cout<<"
存档文件
";
else cout<<"
其它文件
";
cout<<endl;
}
}
_findclose(lf);
cin.get();
return0;
}
代码二、
#include<iostream>
#include<string>
#include<io.h>
using namespace std;
void
visit(string path,intlayer)
{
struct_finddata_t
filefind;
string
curr=path+"\\*.*";
longhandle;
inti;
if((handle=_findfirst(curr.c_str(),&filefind))==-1)return;
while(_findnext(handle,&filefind) ==0)
{
if(!strcmp(filefind.name,"..")){
continue;
}
for(i=1;i<layer;i++)cout<<"
";
if(_A_SUBDIR==filefind.attrib) //是目录
{
curr=path+"\\"+filefind.name;
visit(curr,layer+1);
}
else//不是目录,是文件
{
cout<<path+"\\"+filefind.name<<endl;
}
}
_findclose(handle);
}
int
main()
{
string
path;
cout<< "请输入目录:";
cin>> path;
visit(path,1);
system("PAUSE");
return
0;
}
转载:http://blog.sina.com.cn/s/blog_56d8ea900100yejj.html
首先了解一下一个文件结构体:
struct _finddata_t {
};
time_t,其实就是long
而_fsize_t,就是unsigned long
现在来解释一下结构体的数据成员吧。
attrib,就是所查找文件的属性:_A_ARCH(存档)、_A_HIDDEN(隐藏)、_A_NORMAL(正常)、_A_RDONLY(只读)、_A_SUBDIR(文件夹)、_A_SYSTEM(系统)。
time_create、time_access和time_write分别是创建文件的时间、最后一次访问文件的时间和文件最后被修改的时间。
size:文件大小
name:文件名。
再来看一下_findfirst函数:long _findfirst(const char *, struct _finddata_t*);
第一个参数为文件名,可以用"*.*"来查找所有文件,也可以用"*.cpp"来查找.cpp文件。第二个参数是_finddata_t结构体指针。若查找成功,返回文件句柄,若失败,返回-1。
然后,_findnext函数:int _findnext(long, struct _finddata_t *);
第一个参数为文件句柄,第二个参数同样为_finddata_t结构体指针。若查找成功,返回0,失败返回-1。
最后:_findclose()函数:int _findclose(long);
只有一个参数,文件句柄。若关闭成功返回0,失败返回-1。
代码一、
#include<iostream>
#include<io.h>
using namespace std;
int main()
{
}
代码二、
#include<iostream>
#include<string>
#include<io.h>
using namespace std;
void
{
}
int
{
}
转载:http://blog.sina.com.cn/s/blog_56d8ea900100yejj.html