这是什么时候回来 [英] What Time Is This Returning

查看:36
本文介绍了这是什么时候回来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的酱汁很深.我没有太多时间工作,所以我在这里有点困惑.我知道有 FILETIME 和 SYSTEMTIME.我此时想要得到的(因为它可能会改变)是不到 20 秒的文件.这会在几秒钟内返回文件及其大小和某些内容,我想知道的是它按时间过滤的位置,以及我如何调整它以满足我的需要.谢谢.

Deep in the sauce here. I haven't worked with time to much so I'm a little confused here. I know there is FILETIME and SYSTEMTIME. What I am trying to get at this point (because it might change) are file that are less than a 20 seconds old. This returning the files and their size and something in seconds, What I'd like to know is where it is filtering by time if it is, and how can I adjust it to suit my needs. Thank you.

using namespace std;
typedef vector<WIN32_FIND_DATA> tFoundFilesVector;
std::wstring LastWriteTime;  
int getFileList(wstring filespec, tFoundFilesVector &foundFiles)
{
    WIN32_FIND_DATA findData;
    HANDLE h;
    int validResult=true;

    int numFoundFiles = 0;
    h = FindFirstFile(filespec.c_str(), &findData);

    if (h == INVALID_HANDLE_VALUE)
        return 0;

    while (validResult)
    {
        numFoundFiles++;
        foundFiles.push_back(findData);
        validResult = FindNextFile(h, &findData);
    }
    return numFoundFiles;
}

void showFileAge(tFoundFilesVector &fileList)
{
    unsigned _int64 fileTime, curTime, age;
    tFoundFilesVector::iterator iter;
    FILETIME ftNow;
    //__int64 nFileSize;
          //LARGE_INTEGER li;    
    //li.LowPart = ftNow.dwLowDateTime;
    //li.HighPart = ftNow.dwHighDateTime;

    CoFileTimeNow(&ftNow);
          curTime = ((_int64) ftNow.dwHighDateTime << 32) + ftNow.dwLowDateTime;

          for (iter=fileList.begin(); iter<fileList.end(); iter++)
    {
        fileTime = ((_int64)iter->ftLastWriteTime.dwHighDateTime << 32) + iter->ftLastWriteTime.dwLowDateTime;

        age = curTime - fileTime;

        cout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << "  seconds" << endl;
    }
}

int main()
{
    string fileSpec = "*.*";
    tFoundFilesVector foundFiles;
    tFoundFilesVector::iterator iter;

    int foundCount = 0;

    getFileList(L"c:\\Mapper\\*.txt", foundFiles);
    getFileList(L"c:\\Mapper\\*.jpg", foundFiles);

    foundCount = foundFiles.size();
    if (foundCount)
    {
        cout << "Found "<<foundCount<<" matching files.\n";
        showFileAge(foundFiles);
    }
    system("pause");
    return 0;
}

推荐答案

我不知道你做了什么来尝试调试这个,但你的代码根本不起作用.原因是您将 getFileList() 传递给 wstring,然后将其传递给 FindFirstFile() 的 ANSI 版本.除非您 #define UNICODE 或使用适当的编译器选项,否则所有系统调用都将使用 char *,而不是 UNICODE.

I don't know what you've done to try to debug this but your code doesn't work at all. The reason is you're passing getFileList() a wstring but then passing that to the ANSI version of FindFirstFile(). Unless you #define UNICODE or use the appropriate compiler option, all system calls will expect char *, not UNICODE.

最简单的解决方法是简单地将 getFileList() 的声明更改为:

The easiest fix is to simply change the declaration of getFileList() to this:

int getFileList(const char * filespec, tFoundFilesVector &foundFiles)

将对 FindFirstFile() 的调用更改为:

Change the call to FindFirstFile() to this:

h = FindFirstFile((LPCSTR)filespec, &findData);

然后将对其的调用更改为:

And then change the calls to it to this:

getFileList("c:\\Mapper\\*.txt", foundFiles);
getFileList("c:\\Mapper\\*.jpg", foundFiles);

您的另一个选择是将所有字符字符串切换为宽字符,但无论哪种方式,您都需要始终保持一致.一旦你这样做了,程序就会按预期工作.

Your other option is to switch all char strings to wide chars, but either way you need to be consistent throughout. Once you do that the program works as expected.

至于你的最后一个问题,你的程序根本没有按时间过滤.

As for your final question, your program is not filtering by time at all.

这篇关于这是什么时候回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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