void fun1(char *fl){
//flNamep : stores the path of our directory
DIR *dip;
struct dirent *dit;
dip = opendir(fl);
if (dip==NULL) {cerr<<"Error\n";exit(-1);}
while ((dit=readdir(dip)))
{
string trun = (dit->d_name);
struct stat buff;
stat(dit->d_name, &buff);
if (((buff.st_mode & S_IFREG)==S_IFREG))
{cout<<"File"<<endl;}
else if (((buff.st_mode & S_IFDIR)==S_IFDIR))
{cout<<"Dir"<<endl;}
}
closedir(dip);
}
代码在dir和文件中没有区分。我错过了什么吗?我无法使用Boost或任何其他STL。只有C Posix支持的文件。需要知道我是错的。C++中的stat&S_IFREG
更新代码,每回答
DIR *dip;
struct dirent *dit;
dip = opendir(flNamep);
if (dip==NULL) {cerr<<"Err\n";exit(-1);}
while ((dit=readdir(dip)))
{
string trun = (dit->d_name);
string fullpath = flNamep;
fullpath+='/';
fullpath+=trun;
if((trun==".") || (trun=="..")) {cout<<"";}
else
{
struct stat buff;
stat(dit->d_name, &buff);
if (((buff.st_mode & S_IFDIR)==S_IFDIR))
{cout<<"Dir"<<endl;}
else
{cout<<"File"<<endl;}
}
考虑使用'S_ISREG(buff.st_mode)'和'S_ISDIR(buff.st_mode)'。不是说它影响'_unchecked'stat()'_'问题。 –