ME-dia
ME-dia

Reputation: 289

Map Isn't Returning Correct Numbers

I have a map that is acting up and not returning the correct number. It did then it didn't, now it's just not returning. Any help is appreciated. Thank you.

struct file_data 
{ 
    std::wstring sLastAccessTime; 
    __int64 nFileSize      ; 
};

int GetFileList(const wchar_t *searchkey, std::map<std::wstring, file_data> &map) 
{ 
    WIN32_FIND_DATA fd; 
    HANDLE h = FindFirstFile(searchkey,&fd); 
    if(h == INVALID_HANDLE_VALUE) 
    { 
        return 0; // no files found 
    } 

    while(1) 
    { 
        wchar_t buf[128]; 
        FILETIME ft = fd.ftLastWriteTime; 
        SYSTEMTIME sysTime; 
        FileTimeToSystemTime(&ft, &sysTime); 
        wsprintf(buf, L"%d-%02d-%02d",sysTime.wYear, sysTime.wMonth, sysTime.wDay); 

        file_data filedata; 
        filedata.sLastAccessTime= buf; 
        filedata.nFileSize      = (((__int64)fd.nFileSizeHigh) << 32) + fd.nFileSizeLow; 

        map[fd.cFileName]= filedata; 

        if (FindNextFile(h, &fd) == FALSE) 
            break; 
    } 
    return map.size(); 
} 

int main() 
{ 
    std::map<std::wstring, file_data> map; 
    int count = GetFileList(L"C:\\Users\\DS\\Downloads\\*.pdf", map); 
    int count1 = GetFileList(L"C:\\Users\\DS\\Downloads\\*.txt", map); 
    int count2 = GetFileList(L"C:\\Users\\DS\\Downloads\\*.jpg", map);

    for(std::map<std::wstring, file_data>::const_iterator it = map.begin(); it != map.end(); ++it) 
    {
        if (count2 != 0) 
        { 
            printf("\n   How Many: %i   \n", count2);
        } 
        else 
        { 
            printf ("%s \n", "Nothing");
        } 
        return 0; 
    }
}

Upvotes: 0

Views: 71

Answers (2)

ME-dia
ME-dia

Reputation: 289

OK found the solution. This is it.

GetFileList(L"C:\\Users\\DS\\Downloads\\*.pdf", map);
GetFileList(L"C:\\Users\\DS\\Downloads\\*.txt", map);
GetFileList(L"C:\\Users\\DS\\Downloads\\*.jpg", map);

if( map.size() > 0 
  then...........

Upvotes: 0

Lior Kogan
Lior Kogan

Reputation: 20608

Note that GetFileList() returns the number of items in the map.

In your implementation it is cumulative. Maybe you want to clear the map between consecutive calls to GetFileList().

Upvotes: 1

Related Questions