两种情况下的S_ISDIR如何得到不同的效果

求助:两种情况下的S_ISDIR怎么得到不同的效果?
参考了2种遍历目录的写法,怎么大致上一样的写法却得到了不同的结果?
第一种:不是我想要的结果
int   browse_dir(string   dir)
{
                DIR   *pDir;
                struct   dirent   *pEnt;
                struct   stat   statbuf;
                string   sFileName;

                pDir   =   opendir(dir.c_str());
                if   (pDir   ==   NULL)   {
                                if   (errno   ==   ENOTDIR)   {
                                                filenames.push_back(dir);
                                                return   0;
                                }
                                perror(dir.c_str());
                                return   -1;
                }
                while   ((pEnt=readdir(pDir))!=   NULL)
                {
                                if   ((pEnt-> d_name[0]== '. ')&&(pEnt-> d_name[1]== '. '   ||   pEnt-> d_name[1]==0))
                                {
                                                continue;
                                }
                                lstat(pEnt-> d_name,&statbuf);
                                if(!S_ISREG(statbuf.st_mode))//执行该代码后,无论是目录还是文件,均为true,所以只会打印出directory-> 。。。的结果,还有   如果把!S_ISREG改为S_ISDIR则把无论什么都会打印出file-> …..
                                {
                                        sFileName=   dir   +   "/ "   +   pEnt-> d_name;
                                        printf( "directory-> %s\n ",   sFileName.c_str());
          &

两种情况下的S_ISDIR如何得到不同的效果

相关文章:

你感兴趣的文章:

标签云: