weixin_40011552
且听下回分说
采纳率69.2%
2019-03-17 17:05 浏览 576

把遍历文件夹中所有的文件名写入一个txt文件中保存

自己给出一个文件夹,把该文件夹中所有的文件名写入一个自定义txt文件中保存

现在不能遍历子文件夹

      WIN32_FIND_DATAA fileData;
            char strfile[1024] = {};
            HANDLE hflie = CreateFile(_T("遍历结果.txt"), 
            GENERIC_ALL, FILE_SHARE_READ, nullptr, CREATE_NEW,FILE_ATTRIBUTE_NORMAL, nullptr);

            HANDLE hfiandfile = FindFirstFileA("../遍历文件夹/*.*",&fileData);
            memcpy(strfile, fileData.cFileName, sizeof(char)*strlen(strfile));
            strcat_s(strfile, "\r\n");

            DWORD len = 0;
            WriteFile(hflie, strfile, strlen(strfile), &len, nullptr);

            while (FindNextFileA(hfiandfile, &fileData))
            {
                ZeroMemory(strfile, sizeof(char)* 1024);
                memcpy(strfile, fileData.cFileName, sizeof(char)*strlen(fileData.cFileName));
                strcat_s(strfile, "\r\n");
                WriteFile(hflie, strfile, strlen(strfile), &len, nullptr);
            }

            CloseHandle(hflie);
            CloseHandle(hfiandfile);

            return (INT_PTR)TRUE;
        }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答 默认 最新

  • 已采纳
    benpaodaoren benpaodaoren 2019-03-18 13:29

    遍历子文件夹一般都用递归来做,给你个用递归实现的代码,如果不用递归做会比较麻烦。

    #define MAX_PATH_TEST (1024)
    void RecursionDir(char* pcDir, HANDLE hOutFile)
    {
    char strfile[MAX_PATH_TEST] = {};
    char acFind[MAX_PATH_TEST] = {};
    sprintf(acFind, "%s/*", pcDir);
    WIN32_FIND_DATAA fileData;
    HANDLE hfiandfile = FindFirstFileA(acFind,&fileData);
    if (INVALID_HANDLE_VALUE == hfiandfile)
    {
    return;
    }

    do 
    {
        if (0 == strcmp(fileData.cFileName, ".") || 0 == strcmp(fileData.cFileName, ".."))
        {
            continue;
        }
    
        ZeroMemory(strfile, sizeof(char)* MAX_PATH_TEST);
        memcpy(strfile, fileData.cFileName, sizeof(char)*strlen(fileData.cFileName));
        strcat_s(strfile, "\r\n");
        DWORD len = 0;
        WriteFile(hOutFile, strfile, strlen(strfile), &len, nullptr);
    
        if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            char acChild[MAX_PATH_TEST] = {};
            sprintf(acChild, "%s/%s", pcDir, fileData.cFileName);
            RecursionDir(acChild, hOutFile);
        }
    
    } while (FindNextFileA(hfiandfile, &fileData));
    FindClose(hfiandfile);
    

    }

    int main(){

    WIN32_FIND_DATAA fileData;
    char strfile[1024] = {};
    HANDLE hflie = CreateFile(_T("遍历结果.txt"), 
    GENERIC_ALL, FILE_SHARE_READ, nullptr, CREATE_NEW,FILE_ATTRIBUTE_NORMAL, nullptr);
    
    RecursionDir("../遍历文件夹", hflie);
    
    CloseHandle(hflie);
    
    return 0;
    

    }

    点赞 评论 复制链接分享
  • d2262272d 窝米逗佛~ 2019-03-18 13:46
    bool iterFolder( HANDLE hflie, string strDir )
    {
        WIN32_FIND_DATAA FindData;
        HANDLE hHandle;
    
        char FilePathName[ MAX_PATH ];
        char strfile[ 1024 ] = { };
    
        strcpy_s( FilePathName, MAX_PATH, strDir.c_str() );
        strcat_s( FilePathName, "\\*.*" );
        hHandle = FindFirstFileA( FilePathName, &FindData );
        if ( hHandle == INVALID_HANDLE_VALUE )
        {
            FindClose( hHandle );
            return false;
        }
        // 构造路径
        char FullPathName[ MAX_PATH ];
        while ( ::FindNextFileA( hHandle, &FindData ) )
        {
            // 过虑.和..
            if ( strcmp( FindData.cFileName, "." ) == 0 || strcmp( FindData.cFileName, ".." ) == 0 )
                continue;
    
            if ( FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
            {
                // 构造完整路径
                sprintf( FullPathName, "%s\\%s", strDir.c_str(), FindData.cFileName );
                string strfile = FindData.cFileName;
                strfile += "\r\n";
                DWORD len = 0;
                WriteFile( hflie, strfile.c_str(), strfile.length(), &len, nullptr );
                iterFolder( hflie, FullPathName );      // 遍历子目录
            }
            else
            {
                // 完整文件路径 
                sprintf( FullPathName, "%s\\%s", strDir.c_str(), FindData.cFileName );
                string strfile = FindData.cFileName;
                strfile += "\r\n";
                DWORD len = 0;
                WriteFile( hflie, strfile.c_str(), strfile.length(), &len, nullptr );
            }
        }
        FindClose( hHandle );
        return true;
    }
    
    int main()
    {
          WIN32_FIND_DATAA fileData;
          char strfile[ 1024 ] = { };
          HANDLE hflie = CreateFileA( ( "遍历结果.txt" ),
            GENERIC_ALL, FILE_SHARE_READ, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr );
            iterFolder( hflie, "." );
            CloseHandle( hflie );
            return ( INT_PTR )TRUE;
    }
    
    点赞 评论 复制链接分享

相关推荐