地图计数错误 [英] Map Giving Wrong Count

查看:70
本文介绍了地图计数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用地图,正在计数.但首先,我的目录中有2个pdf文件,
6个文本文件,和0个jpg文件= 8个文件.现在,当我只用数做printf时,我得到2 ...这是正确的.当我用count1做printf时,我得到8是正确的.然而;当我用count2做printf时,我得到12 ....错误!为什么会这样?
代码.

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.

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
printf("\n Delete: %i \n", count1); = 8仅pdf和txt
printf("\n Delete: %i \n", count2); = 12什么?为什么?

谢谢!查克(Chuck)我以为我会公开露面,这样像我这样的其他人也可以从中受益.谢谢Chuck O''Toole的慷慨帮助!
地板开着!!!!



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!!!!

推荐答案

为什么会这样?
因为GetFileList(L"C:\\Users\\DS\\Downloads\\*.jpg", map);返回12 ...
如果我们想为您提供帮助,则您需要提供实际失败的代码.而且,我什至会说map没有给出错误的计数,但是您必须插入元素.
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.


使用调试器并跟踪代码.如果您有大约12个文件,则不会花太多时间查看出什么问题.

或者,在将其添加到地图时输出诸如文件名之类的字符串,或者在获取文件列表时显示地图内容.

学会使用这些工具,您将花费更少的精力去发现错误,一旦发现它无法按预期工作的地方,您就可以考虑如何解决它.

在大多数情况下,通过使用调试器或打印一些信息,很容易发现问题.
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.


FindFirstFile [ WIN32_FIND_DATA结构的dwFileAttributes属性 [ ^ ],然后检查FILE_ATTRIBUTE_HIDDENFILE_ATTRIBUTE_SYSTEM属性.

替换
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
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

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;
}


这篇关于地图计数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆