We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,687 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Map Not Returning Correct Count

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; 
}
}
2
Contributors
1
Reply
3 Days
Discussion Span
1 Year Ago
Last Updated
2
Views
Question
Answered
DSTR3
Newbie Poster
8 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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();
}
MonsieurPointer
Junior Poster
143 posts since Jun 2011
Reputation Points: 45
Solved Threads: 15
Skill Endorsements: 0
Question Answered as of 1 Year Ago by MonsieurPointer

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0910 seconds using 2.79MB