Windows 地图不是';t返回正确的数字
我有一张地图坏了,没有返回正确的号码。它有,然后没有,现在它只是不回来。感谢您的帮助。多谢各位Windows 地图不是';t返回正确的数字,windows,visual-c++,Windows,Visual C++,我有一张地图坏了,没有返回正确的号码。它有,然后没有,现在它只是不回来。感谢您的帮助。多谢各位 struct file_data { std::wstring sLastAccessTime; __int64 nFileSize ; }; int GetFileList(const wchar_t *searchkey, std::map<std::wstring, file_data> &map) { WIN32_FIND_D
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 How Many: %i \n", count2);
}
else
{
printf ("%s \n", "Nothing");
}
return 0;
}
}
struct file\u数据
{
std::wstring sLastAccessTime;
__int64-nFileSize;
};
int GetFileList(常量wchar\u t*searchkey,std::map&map)
{
WIN32_FIND_DATA fd;
HANDLE h=FindFirstFile(searchkey,&fd);
if(h==无效的\u句柄\u值)
{
返回0;//未找到任何文件
}
而(1)
{
wchar_t buf[128];
FILETIME 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)注意GetFileList()
返回映射中的项数
在您的实现中,它是累积的。可能您希望清除对GetFileList()
确定的连续调用之间的映射,以找到解决方案。就是这样
GetFileList(L"C:\\Users\\DS\\Downloads\\*.pdf", map);
GetFileList(L"C:\\Users\\DS\\Downloads\\*.txt", map);
GetFileList(L"C:\\Users\\DS\\Downloads\\*.jpg", map);
if( map.size() > 0
then...........
我们要调试你的无知吗?修正你的缩进!好的,修正了!阿德里安,你能帮我吗?谢谢!你称之为“修正”??现在,我确实修复了它!再修复一些。缩进仍然是一团乱。另外,请解释一下你期望的和你实际得到的。最后的循环没有多大意义。你实际上要为地图中的每个项目打印一次地图中的项目总数。这似乎是完全相同的程序因为你不能只说“没有返回正确的号码”因为你没有说正确的数字是多少。你需要说你期望的和你实际得到的。是的,它是累积的。我只想知道三种文件类型的总数。现在我得到7,应该是3。我如何解决这个问题?谢谢你,Lior。Lior,尝试了这个,但它没有返回任何结果。std::map;int count=get文件列表(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);map.clear();什么意思是“没有返回”count
、count1
和count2
都是零?它返回空值…但我有解决方案。我必须等一个小时…太新了。谢谢。