1,105,534 Community Members

Map Not Returning Correct Count

Member Avatar
DSTR3
Newbie Poster
8 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have a map that is not returning the correct number. First it did, then it didn't now it's just messed up. Any help is appreciated.Thanks.

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   Delete: %i   \n", count2);
		} 
		else 
		{ 
		printf ("%s \n", "Nothing");
		} 
	system("pause");
    return 0; 
}
}
Member Avatar
MonsieurPointer
Junior Poster
152 posts since Jun 2011
Reputation Points: 27 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
0
 

You are forgetting to close your file handle. Add FindClose(h); before return map.size(); (added in code block below).

Also, you are passing the same map (map) in each call, meaning the number will constantly increase in size. Is that by design? If not, you will need to clear the map before each call:

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

Or, alternatively (and easier to maintain - change is in line #9):

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
}
map.clear();
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;
}
FindClose(h);
return map.size();
}
Question Answered as of 2 Years Ago by MonsieurPointer
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: