得到指定目录下的子文件夹列表, 这个程序哪儿有错?
总是获得的子文件夹列表中少了一个文件夹
VOID CServerFile::GetDirDirList(char* sPath, list<CString>& strList)
{
string str;
str = sPath;
str.append(_T("\\*.*"));

CFileFind mFileFind;

CString strTemp;

BOOL bRet = mFileFind.FindFile(str.c_str());
if(bRet) bRet = mFileFind.FindNextFile();
else return; while(bRet)
{
strTemp = mFileFind.GetFileName();
if((mFileFind.IsDirectory()) && (!mFileFind.IsDots()) ) 
{
strList.push_back(strTemp);
}
bRet = mFileFind.FindNextFile();
}
mFileFind.Close();
}

解决方案 »

  1.   

    提供给你一段递归进入目录的例子 void Search::EnumerateFolder(char * path) 

    WIN32_FIND_DATA fd; 
    char folder[MAX_PATH]; 
    int i; if (path == NULL) 
    return; memset(folder, 0, MAX_PATH); 
    sprintf(folder, "%s\\*.*", path); 
        HANDLE hFind  =  ::FindFirstFile (folder,  & fd ); 
         
         if  (hFind  !=  INVALID_HANDLE_VALUE)  
         { 
             do   
             { 
                 if  ( fd.dwFileAttributes  &  FILE_ATTRIBUTE_DIRECTORY )  
                 { 
                    CString name  =  fd.cFileName; 
                    if  ( name  !=  _T (".")  &&  name  !=  _T("..") )  
                    { 
    char subfolder[MAX_PATH]; 
    memset(subfolder, 0, MAX_PATH); 
    sprintf(subfolder, "%s\\%s", path, fd.cFileName); 
                        TRACE ( _T (  " Folder: %s\n "  ), subfolder ); 
                        //::SetCurrentDirectory ( fd.cFileName ); 
                        EnumerateFolder (subfolder); 
                        ::SetCurrentDirectory (_T ("..")); 
                    } 
                } 
                else 
                { TRACE ( _T (  "File: %s\n "  ), fd.cFileName ); 
                } 
            }   while ( ::FindNextFile ( hFind,  & fd) ); 
            ::FindClose ( hFind ); 
     } 
    }
      

  2.   

    本帖最后由 Mackz 于 2008-04-26 16:47:53 编辑
      

  3.   


    使用这个系统的API的方式,确实是正确的...但是我还是想弄清楚,为什么我那样做就会有错... 郁闷死了...
      

  4.   


    -_-#  不要忽悠我哦...其实在这里这个while do 和do while 是一样的...
    他两的唯一不一样处,是在于最初是否判断哦
      

  5.   

    VOID CServerFile::GetDirDirList(char* sPath, list <CString>& strList) 

    string str; 
    str = sPath; 
    str.append(_T("\\*.*")); CFileFind mFileFind; CString strTemp; BOOL bRet ;
    do{
     bRet= mFileFind.FindFile(str.c_str()); 
     if(bRet) 
       bRet = mFileFind.FindNextFile(); 
     else break; 
     strTemp = mFileFind.GetFileName(); 
     if((mFileFind.IsDirectory()) && (!mFileFind.IsDots()) )  
     { 
       strList.push_back(strTemp); 
     } 
     bRet = mFileFind.FindNextFile(); 
     } while(bRet)
     mFileFind.Close(); 
    }