Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm using a map, it's counting. But first, in my directory I have 2 pdf files,
6 text files, and 0 jpg files = 8 total. Now when I do a printf with just count I get 2...which is correct. When I do printf with count1 I get 8 which is correct. However; when I do printf with count2 I get 12....WRONG! Why is this happening?
The code.
C++
std::map 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);


printf("\n Delete: %i \n", count); = 2 pdf only
printf("\n Delete: %i \n", count1); = 8 pdf and txt only
printf("\n Delete: %i \n", count2); = 12 what? why?

Thank you! Chuck I thought tht I would bring this out in the open so that others like me might benefit from this. Thank you, Chuck O'Toole for your gracious help!
The floor is open!!!!
Posted
Updated 29-Sep-11 18:28pm
v2
Comments
Chuck O'Toole 30-Sep-11 0:03am    
Please remember that hundreds of questions are posted everyday to these forums. Not everybody remembers all the questions you've asked before where you've already shown the code. So, my advise is to realize that each new question may be read by a totally different group of people, none of which have seen "GetFileList()", which is not some magic library function but something in your "unseen" code. Each question should be viewed as a standalone question and not the continuation of some other question.
Member 7766180 30-Sep-11 0:13am    
Oh I thought GetFileList() was universal,like "French Fries".....:) Sorry. You DO have a VERY valid point and I will keep this in mind. Thank you, Chuck.
Chuck O'Toole 30-Sep-11 0:14am    
"French Fries" are not universal either. Hint, just what are "Chips" as in "Fish and Chips", ask a Brit :)
Richard MacCutchan 30-Sep-11 5:10am    
Food of the gods!
Member 7766180 30-Sep-11 15:58pm    
Yesss! Now can the codes help me figure out why my map is wackey? I have Chips and Fries!!!! :)

Why is this happening?
because GetFileList(L"C:\\Users\\DS\\Downloads\\*.jpg", map); returns 12...
if we are to try and help you you need to provide the code which actually fails. And I will go as far as to say map does not give the wrong count, but you must be inserting the elements.
 
Share this answer
 
Comments
Member 7766180 29-Sep-11 23:24pm    
OK Simon I tested further. I think I found the problem. When I change the code and just search jpg I get 4.....When I go in explorer and search I find 0, When I search from the console window I get 0. Does Get File find hidden files? I have show hidden files turned on and there are none? Whats the deal? And can I remove the count from the two top ones and just have count on the bottom GetFileList() Thanks.
Simon Bang Terkildsen 30-Sep-11 0:51am    
As I said I can't help you futher when you haven't posted the actual code for GetFileList
Member 7766180 30-Sep-11 1:01am    
Sorry, here it is Simon.

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;
GetFileList(L"C:\\Users\\DS\\Downloads\\*.jpg", map);
GetFileList(L"C:\\Users\\DS\\Downloads\\*.txt", map);
int count = GetFileList(L"C:\\Users\\DS\\Downloads\\*.pdf", map);

for(std::map<std::wstring, file_data="">::const_iterator it = map.begin();
it != map.end(); ++it)
{
/////////////////////////////////////////////////////////////////
if (count != 0)
{
printf("\n Delete: %i \n", count);
}
else
{
printf ("%s \n", "Nothing");
/////////////////////////////////////////////////////////////////
}
system("pause");
return 0;
}
}
Member 7766180 30-Sep-11 2:18am    
The other problem is the GetFileList() group if the first or second ones are null then it returns nothing even if the third instance has a count. Somethings wrong!
Espen Harlinn 1-Oct-11 9:11am    
Good points :)
Uses a debugger and trace the code. If you have about 12 files, it won't take to much time to see what wrong.

Or alternatively, output some string like the file name when you add it to the map or display the map content when getting the file list.

Learn to uses these tools and you will find your errors with much less effort and once you have found where it does not work as intented, the you can think on how to fix it.

Most of the time, it would be easy to see the problem by either using the debugger or printing some information so that you know what really happen.
 
Share this answer
 
v3
Comments
Member 7766180 30-Sep-11 21:32pm    
Thank you. Philippe. I appreciate the input. I think that the problem I'm having is that C++ is a much lower language than VB and a lot of things that were done for me in VB are not done in C++, plus the syntax is way different. I've been hammering away at this part-time for six months now and I know I still have along way to go................
Espen Harlinn 1-Oct-11 9:13am    
5'ed!
FindFirstFile[^] also finds hidden and system files, which are probably the additional file you are getting. You need to filter these out if you do not want them.

Use the dwFileAttributes attribute of the WIN32_FIND_DATA structure[^] and check for the FILE_ATTRIBUTE_HIDDEN and FILE_ATTRIBUTE_SYSTEM attributes.

Replace
C++
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;
}

with
C++
file_data filedata;
SYSTEMTIME sysTime;
FILETIME ft;
wchar_t buf[128];

while(1)
{
	if( fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN != FILE_ATTRIBUTE_HIDDEN && fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM != FILE_ATTRIBUTE_SYSTEM )
	{
		ft = fd.ftLastWriteTime;

		FileTimeToSystemTime(&ft, &sysTime);

		wsprintf(buf, L"%d-%02d-%02d",sysTime.wYear, sysTime.wMonth, sysTime.wDay);
		filedata.sLastAccessTime= buf;
		filedata.nFileSize = (((__int64)fd.nFileSizeHigh) << 32) + fd.nFileSizeLow;

		map[fd.cFileName]= filedata;
	}

	if (FindNextFile(h, &fd) == FALSE) 
		break;
}
 
Share this answer
 
v3
Comments
Member 7766180 30-Sep-11 19:53pm    
Thank you again Andre! You are one a roll! Want to go for a hat trick! :))Help me with...http://www.codeproject.com/Questions/262182/Filtering-Time-With-FindFile Thank you so much........for the extra treat of hidden files! That helps a lot!
Chuck O'Toole 30-Sep-11 20:47pm    
This is why I've decided to stop helping you once again. After 2 days of messages, walking you through a method where you can do your own debugging, and just trying to teach you as you've always said you wanted to learn this stuff, you still just want somebody to do the work for you. What help could Andre give you beyond what you've been given, other than to just write the code for you. Even here, you got Andre to finally post a replacement for your code. Now you have something to paste, which I suspect is all you really want, somebody to do it for you. Andre, read all the messages in the question thread he's pointing you to and decide if you really want to go down this time-sink with this guy.
Chuck O'Toole 30-Sep-11 20:56pm    
And if you need further evidence that he's a time-sink, http://www.codeproject.com/Answers/261623/How-To-Get-A-Count-Of-Files
How many days can you spend banging on that one piece of code, getting lots of help and instruction on how to program this, and still have no clue about how to do it yourself? I no longer wish to play this game.
Espen Harlinn 1-Oct-11 9:12am    
5'ed!
Richard MacCutchan 1-Oct-11 12:16pm    
I think it's Chuck that deserves the 5s (quite a few)!
A bit of reading and this: Boost Filesystem[^], should help you out nicely.

You should perhaps consider taking a look at Thinking in C++ 2nd Edition[^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Simon Bang Terkildsen 1-Oct-11 10:00am    
My 5, I didn't know about Boost, which I'm now going to use in my current project, thank you.
Espen Harlinn 1-Oct-11 10:27am    
There are all sorts of goodies in this collection of libraries, some stuff have even started out as boost libraries and worked it's way into the c++ standard :)
Member 7766180 1-Oct-11 14:53pm    
Thank you, Espen. I will look into it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900