我要搜索文件a.txt 可我不知道a.txt在哪个目录 搜索时当搜索到文件时比较文件名后进入搜索下一文件或目录 搜索到目录时进入目录搜索  请问我如何区分当前我搜索到的是目录还是文件呢

解决方案 »

  1.   

    摘自MSDN
    #include <afx.h>
    #include <iostream>using namespace std;void Recurse(LPCTSTR pstr)
    {
       CFileFind finder;   // build a string with wildcards
       CString strWildcard(pstr);
       strWildcard += _T("\\*.*");   // start working for files
       BOOL bWorking = finder.FindFile(strWildcard);   while (bWorking)
       {
          bWorking = finder.FindNextFile();      // skip . and .. files; otherwise, we'd
          // recur infinitely!      if (finder.IsDots())
             continue;      // if it's a directory, recursively search it      if (finder.IsDirectory())
          {
             CString str = finder.GetFilePath();
             cout << (LPCTSTR) str << endl;
             Recurse(str);
          }
       }   finder.Close();
    }
      

  2.   

    如果是用MFC的话,楼上的就可以了如果要使用API来做的文件搜索
    DWORD GetFileAttributes(
      LPCTSTR lpFileName   // name of file or directory
    );if(GetFileAttributes(_T("文件名")) & FILE_ATTRIBUTE_DIRECTORY)
    {
        // 目录
    }
    else
    {
        // 文件
    }