Map Returning Wrong Count

I have this map that was working, maybe not. In any case it's not returning the correct number. Any help is appreiated. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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; 
}
}
Last edited on
1. You are leaking resources: You are not closing the find handle. Use FindClose() to close it. I suggest a simple class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class FindFiles
{
private:
    HANDLE _hFind;
    std::wstring _searchKey;

public:
    FindFiles(const wchar_t *searchKey) : _hFind(NULL), _searchKey(searchKey)
    { }
    ~FindFiles()
    {
        Close();
    }

public:
    BOOL Find(WIN32_FIND_DATAW &fd)
    {
        Close();
        _hFind = FindFirstFileW(_searchKey, &fd);
        //2Do:  Check for errors.
        return (_hFind == NULL ? FALSE : TRUE);
    }
    BOOL FindNext(WIN32_FIND_DATAW &fd)
    {
        if (_hFind == NULL) { //Throw exception or something. }
        return FindNextFileW(_hFind, &fd);
    }
    void Close()
    {
        if (_hFind == NULL) return;
        FindClose(_hFind);
        _hFind = NULL;
    }
};


Note how in the above code I explicitly use the Wide versions of WIN32_FIND_DATA and the functions FindFirstFile() and FindNextFile(). This is the correct way if you want to explicitly work with Unicode.

2. Your FOR loop in main() doesn't look right. If you want to print counts, you don't need the loop. Just print the count.

3. Define what you mean when you say your map is not working.
Last edited on
I need it to return a count of the total number of files found of the mapped types. txt, pdf, jpg and right now it is not returning the correct number. Good catch with the leak! How would I include this class in my code? I am still quite new to this. Thank you. I appreciate the help!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int GetFileList(const wchar_t *searchkey, std::map<std::wstring, file_data> &map) 
{
    WIN32_FIND_DATA fd;
    FindFiles finder(searchkey);
    if (!finder.Find(fd))
    {
        //No files found.
        return 0;
    }
    while (true)
    {
        //Your code here
        ....
        //Now change your line 29 to read:
        if (!finder.FindNext(fd))
        {
            break;
        }
    }
    return map.size();
}


Note that I had to make a small change to the member function FindFiles::Find().

Besides that, the code should work. Debug it. See which files are being enumerated in order to find the reason of the allegedly incorrect count.
This is the problem...If I run any of them seperatly I get the correct number.

//int count = GetFileList(L"C:\\Users\\DS\\Downloads\\*.pdf", map);
int count1 = GetFileList(L"C:\\Users\\DS\\Downloads\\*.txt", map); Returns 3, Correct
//int count2 = GetFileList(L"C:\\Users\\DS\\Downloads\\*.jpg", 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); Returns 5, InCorrect

int count = GetFileList(L"C:\\Users\\DS\\Downloads\\*.pdf", map); No Records, Console Flashes Closed
//int count1 = GetFileList(L"C:\\Users\\DS\\Downloads\\*.txt", map);
//int count2 = GetFileList(L"C:\\Users\\DS\\Downloads\\*.jpg", map);

I think One I need to add a NULL to each one if there are no records.
Problem Solved.

1
2
3
4
5
GetFileList(L"C:\\Mapper\\*.jpg", map);
	GetFileList(L"C:\\Mapper\\*.txt", map);
	GetFileList(L"C:\\Mapper\\*.pdf", map);
	{
		if (map.size() > 0) 
Topic archived. No new replies allowed.