#include <tchar.h>
#include <Windows.h>
#include <string>
#include <strsafe.h>#define  BUFSIZE 4096
HANDLE hFile;
WIN32_FIND_DATA Filedata;TCHAR * Recurse(TCHAR* strDir);
BOOL FindFile(std::wstring strDir);int _tmain()
{
Recurse(_T("D:\\Torque\\TGEA_1_7_1\\engine\\"));
return 0;
}TCHAR *  Recurse(TCHAR* strDir)
{
TCHAR strPathName[BUFSIZE];
TCHAR strTempPath[BUFSIZE];
TCHAR strTemp[256];
ZeroMemory(strPathName,sizeof(TCHAR)*BUFSIZE);
ZeroMemory(strTempPath,sizeof(TCHAR)*BUFSIZE);
ZeroMemory(strTemp,sizeof(TCHAR)*256);
ZeroMemory(&Filedata,sizeof(Filedata));
HRESULT hr;
BOOL bSearchFile = FALSE;
BOOL bFinished = FALSE;
    wcscpy(strPathName,strDir);
if (strPathName[wcslen(strPathName)-1] != _T('\\'))
{
        wcscat(strPathName,_T("\\"));
}
wcscat(strPathName,_T("*"));
bSearchFile = FindFile(strPathName);
if ((Filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wcscpy(strTempPath,strDir);
wcscat(strTempPath,Filedata.cFileName);
wprintf(_T(" %s\n"),Filedata.cFileName);
Recurse(strTempPath);
}
else if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wprintf(_T(" %s\n"),Filedata.cFileName);
}
while (!bFinished)
{
if (FindNextFile(hFile,&Filedata))
{
if ((Filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wcscpy(strTempPath,strDir);
wcscat(strTempPath,Filedata.cFileName);
wcscat(strTempPath,_T("\\"));
wprintf(_T(" %s\n"),Filedata.cFileName);
Recurse(strTempPath);
}
else if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wprintf(_T(" %s\n"),Filedata.cFileName);
}
}
else
{
if( GetLastError() == ERROR_NO_MORE_FILES ) 
{
bFinished = TRUE;
}
else
{
bFinished = TRUE;    
}
}
}
//FindClose(hFile);
return strTempPath;
}BOOL FindFile(std::wstring strDir)
{
hFile = FindFirstFile(strDir.c_str(),&Filedata);
if (hFile == INVALID_HANDLE_VALUE) 
{
printf ("Invalid File Handle. GetLastError reports %d\n", 
GetLastError ());
return FALSE;

else
{
return TRUE;
}
}结果是只遍历了当前文件下的一个目录的所有列表,请问问题在哪?谢谢

解决方案 »

  1.   

            wcscpy(strTempPath,strDir);
            wcscat(strTempPath,TEXT("\\")); // <-加上这个
            wcscat(strTempPath,Filedata.cFileName);
      

  2.   

    最主要的问题是:递归函数不能使用全局变量(只读除外)。
    另有一个问题隐患:数组长度4096比较大,不宜用局部数组,特别是在递归函数中,容易引起栈溢出,应用new分配,函数返回前delete。
      

  3.   

    很显示, 你递归子目录时,目录名是错的
    if ((Filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
                {
                    wcscpy(strTempPath,strDir);
                    wcscat(strTempPath,_T("\\")); //加上这句
                    wcscat(strTempPath,Filedata.cFileName);
                    wcscat(strTempPath,_T("\\"));
                    wprintf(_T(" %s\n"),Filedata.cFileName);
                    Recurse(strTempPath);
                }
      

  4.   

    #include <tchar.h>
    #include <Windows.h>#define  BUFSIZE 4096VOID Recurse(TCHAR* strDir);int _tmain()
    {
    Recurse(_T("D:\\Torque\\TGEA_1_7_1\\engine\\"));
    return 0;
    }VOID  Recurse(TCHAR* strDir)
    {
    HANDLE hFile; 
    WIN32_FIND_DATA Filedata;
    TCHAR *strPathName = new TCHAR[4096];
    TCHAR *strTempPath = new TCHAR[4096];
    ZeroMemory(&Filedata,sizeof(Filedata));
    BOOL bSearchFile = FALSE;
    BOOL bFinished = FALSE;    wcscpy(strPathName,strDir);
    if (strPathName[wcslen(strPathName)-1] != _T('\\'))
    {
            wcscat(strPathName,_T("\\"));
    }
    wcscat(strPathName,_T("*.*"));
    bSearchFile = (INVALID_HANDLE_VALUE !=(hFile = FindFirstFile(strPathName,&Filedata)));
    if (bSearchFile)
    {
    if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
    {
    wprintf(_T(" %s\n"),Filedata.cFileName);
    }
    while (!bFinished)
    {
    if (FindNextFile(hFile,&Filedata))
    {
    if ((Filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
    {
    wcscpy(strTempPath,strDir);
    wcscat(strTempPath,Filedata.cFileName);
    wcscat(strTempPath,_T("\\"));
    wprintf(_T(" %s\n"),Filedata.cFileName);
    Recurse(strTempPath);
    }
    else if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
    {
    wprintf(_T(" %s\n"),Filedata.cFileName);
    }
    }
    else
    {
    if( GetLastError() == ERROR_NO_MORE_FILES ) 
    {
    bFinished = TRUE;
    }
    else
    {
    bFinished = TRUE;    
    }
    }
    }
    }
    else
    {
    wprintf (_T("Invalid File Handle. GetLastError reports %d\n"), 
    GetLastError ());
    }
    delete []strPathName;
    delete []strTempPath;
    FindClose(hFile);
    }
    OK?
      

  5.   


    #include <tchar.h>
    #include <Windows.h>#define  BUFSIZE 4096VOID Recurse(TCHAR* strDir);int _tmain()
    {
    Recurse(_T("D:\\Torque\\TGEA_1_7_1\\engine\\"));
    return 0;
    }VOID  Recurse(TCHAR* strDir)
    {
    HANDLE hFile; 
    WIN32_FIND_DATA Filedata;
    TCHAR *strPathName = new TCHAR[4096];
    TCHAR *strTempPath = new TCHAR[4096];
    ZeroMemory(&Filedata,sizeof(Filedata));
    BOOL bSearchFile = FALSE;
    BOOL bFinished = FALSE;    wcscpy(strPathName,strDir);
    if (strPathName[wcslen(strPathName)-1] != _T('\\'))
    {
            wcscat(strPathName,_T("\\"));
    }
    wcscat(strPathName,_T("*.*"));
    bSearchFile = (INVALID_HANDLE_VALUE !=(hFile = FindFirstFile(strPathName,&Filedata)));
    if (bSearchFile)
    {
    if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
    {
    wprintf(_T(" %s\n"),Filedata.cFileName);
    }
    while (!bFinished)
    {
    if (FindNextFile(hFile,&Filedata))
    {
    if ((Filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
    {
    wcscpy(strTempPath,strDir);
    wcscat(strTempPath,Filedata.cFileName);
    wcscat(strTempPath,_T("\\"));
    wprintf(_T(" %s\n"),Filedata.cFileName);
    Recurse(strTempPath);
    }
    else if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
    {
    wprintf(_T(" %s\n"),Filedata.cFileName);
    }
    }
    else
    {
    if( GetLastError() == ERROR_NO_MORE_FILES ) 
    {
    bFinished = TRUE;
    }
    else
    {
    bFinished = TRUE;    
    }
    }
    }
    }
    else
    {
    wprintf (_T("Invalid File Handle. GetLastError reports %d\n"), 
    GetLastError ());
    }
    delete []strPathName;
    delete []strTempPath;
    FindClose(hFile);
    }
      

  6.   

    #include <tchar.h>
    #include <Windows.h>
    #include <vector>
    #include <string>#define  BUFSIZE          4096
    #define  LINEMAX          125
    #define  FILE_VERSION     _T("1.1")struct USER_DEFINE_FIND_DATA : public  WIN32_FIND_DATA
    {
    std::wstring strFilePath;
    std::wstring strFileVersin;

    USER_DEFINE_FIND_DATA();
    };USER_DEFINE_FIND_DATA::USER_DEFINE_FIND_DATA():
    strFileVersin(FILE_VERSION)
    {}
    std::vector<std::wstring> vFileList;
    std::vector<std::wstring> vDirectoryList;
    std::vector<USER_DEFINE_FIND_DATA> vFileInfo;VOID Recurse(TCHAR* strDir);
    BOOL WinWriteFile(HANDLE hHandle,LPCTSTR strBuffer,DWORD dwNumberOfBytesToWrite = LINEMAX,DWORD dwNumberOfBytesWritten = 0);int _tmain()
    {
    //Recurse(_T("D:\\Torque\\TGEA_1_7_1\\engine\\"));
        Recurse(_T(".\\"));
    return 0;
    }VOID  Recurse(TCHAR* strDir)
    {
    HANDLE hFindFile = INVALID_HANDLE_VALUE; 
    HANDLE hFileUpdateTxt = INVALID_HANDLE_VALUE;
    USER_DEFINE_FIND_DATA Filedata;
    ZeroMemory(&Filedata,sizeof(Filedata));
    TCHAR *strPathName = new TCHAR[BUFSIZE];
    TCHAR *strTempPath = new TCHAR[BUFSIZE];
    BOOL bSearchFile = FALSE;
    BOOL bFinished = FALSE;
    DWORD dwByteWrite = 0;
    hFileUpdateTxt = CreateFile(_T("c:\\update.txt"),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    if (INVALID_HANDLE_VALUE == hFileUpdateTxt)
    {
    wprintf(_T("CreateFile failed at %d"),GetLastError());
    }
    wcscpy(strPathName,strDir);
    if (strPathName[wcslen(strPathName)-1] != _T('\\'))
    {
            wcscat(strPathName,_T("\\"));
    }
    wcscat(strPathName,_T("*.*"));
    bSearchFile = (INVALID_HANDLE_VALUE !=(hFindFile = FindFirstFile(strPathName,&Filedata)));
    if (bSearchFile)
    {
    if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
    {
    wprintf(_T(" %s\n"),Filedata.cFileName);
    }
    while (!bFinished)
    {
    if (FindNextFile(hFindFile,&Filedata))
    {
    if ((Filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
    {
    wcscpy(strTempPath,strDir);
    wcscat(strTempPath,Filedata.cFileName);
    wcscat(strTempPath,_T("\\"));
    wprintf(_T(" %s\n"),Filedata.cFileName);
    vFileInfo.push_back(Filedata); WriteFile(hFileUpdateTxt,Filedata.cFileName,LINEMAX,&dwByteWrite,NULL);
    Recurse(strTempPath);
    }
    else if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
    {
    wprintf(_T(" %s\n"),Filedata.cFileName);
    vFileInfo.push_back(Filedata);
    WriteFile(hFileUpdateTxt,Filedata.cFileName,LINEMAX,&dwByteWrite,NULL);
    }
    }
    else
    {
    if( GetLastError() == ERROR_NO_MORE_FILES ) 
    {
    bFinished = TRUE;
    }
    else
    {
    bFinished = TRUE;    
    }
    }
    }
    }
    else
    {
    wprintf (_T("Invalid File Handle. GetLastError reports %d\n"), 
    GetLastError ());
    }
    delete []strPathName;
    delete []strTempPath;
    FindClose(hFindFile);
    }BOOL WinWriteFile(HANDLE hHandle,LPCTSTR strBuffer,DWORD dwNumberOfBytesToWrite,DWORD dwNumberOfBytesWritten )
    { if (!WriteFile(hHandle,strBuffer,dwNumberOfBytesToWrite,&dwNumberOfBytesToWrite,NULL))
    {
    wprintf (_T("WriteFile failed at %d\n"),GetLastError());
    return FALSE;
    }
    return TRUE;
    }