Windows 添加到地图

Windows 添加到地图,windows,visual-c++,Windows,Visual C++,我正在尝试将文件大小添加到此地图。看起来我把事情搞得一团糟。感谢您的帮助。多谢各位 int GetFileList(const wchar_t *searchkey, std::map<std::wstring, std::wstring> &map) { WIN32_FIND_DATA fd; HANDLE h = FindFirstFile(searchkey,&fd); if(h == INVALID_HANDLE_VALUE)

我正在尝试将文件大小添加到此地图。看起来我把事情搞得一团糟。感谢您的帮助。多谢各位

int GetFileList(const wchar_t *searchkey, std::map<std::wstring, std::wstring> &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);
        map[fd.cFileName] = buf;
  map[fd.nFileSizeHigh] = buf;
  map[fd.nFileSizeLow] = buf;
if(FindNextFile(h, &fd) == FALSE)
      break;
    }
    return map.size();
}

void main()
{ 
    std::map<std::wstring, std::wstring> map;
    int count = GetFileList(L"C:\\Users\\DS\\Downloads\\*.zip", map)
    && GetFileList(L"C:\\Users\\DS\\Downloads\\*.txt", map);
    for(std::map<std::wstring, std::wstring>::const_iterator it = map.begin(); 
          it != map.end(); ++it)
    {
        //MessageBoxW(NULL,it->first.c_str(),L"File Name",MB_OK);
  //MessageBoxW(NULL,it->second.c_str(),L"File Date",MB_OK);

    }
}
int GetFileList(const 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);
map[fd.cFileName]=buf;
map[fd.nFileSizeHigh]=buf;
map[fd.nFileSizeLow]=buf;
if(FindNextFile(h,&fd)==FALSE)
打破
}
返回map.size();
}
void main()
{ 
地图;
int count=GetFileList(L“C:\\Users\\DS\\Downloads\\\*.zip”,map)
&&GetFileList(L“C:\\Users\\DS\\Downloads\\*.txt”,map);
对于(std::map::const_迭代器it=map.begin();
it!=map.end();++it)
{
//MessageBoxW(NULL,it->first.c_str(),L“File Name”,MB_OK);
//MessageBoxW(NULL,it->second.c_str(),L“文件日期”,MB_OK);
}
}

好吧,您需要决定映射源和映射目标

可能您想从文件名映射到结构{file size,file time}

使其与您的代码相似:

struct file_data
{
    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\\*.zip", map);
    GetFileList(L"C:\\Users\\DS\\Downloads\\*.txt", map);

    for(std::map<std::wstring, file_data>::const_iterator it = map.begin();
        it != map.end(); ++it)
    {
        MessageBoxW(NULL,it->first.c_str(),L"File Name",MB_OK);
        MessageBoxW(NULL,it->second.sLastAccessTime.c_str(),L"File Date",MB_OK);
    }

    return 0;
}
struct file\u数据
{
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=((((u_int64)fd.nFileSizeHigh)first.c_str(),L“File Name”,MB_OK);
MessageBoxW(NULL,it->second.sLastAccessTime.c_str(),L“文件日期”,MB_OK);
}
返回0;
}

是文件大小还是文件修改时间?我想两者都要,但此时大小就可以了。谢谢你,你从来没有接受过答案。回去吧,谢谢你,我会试试的。嘿,你和我的朋友同名!:)