首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将FILETIME转换为字符串,否则

将FILETIME转换为字符串,否则
EN

Stack Overflow用户
提问于 2014-01-23 16:47:21
回答 1查看 6.2K关注 0票数 2

我想存储目录中每个文件的文件名和创建时间。我找到了下面的代码,它浏览一个目录并检测每个文件。问题是我不知道如何存储来自WIN32_FIND_DATA的值。cFilename不应该太难,它是TCHAR (但我是C++的初学者),而ftCreationTime是一个结构,所以它不能存储到向量中,因为它没有构造函数。

这段代码的最终目标是检测是否在目录中创建了一个具有相同内容的新文件。一些图片是由软件定期创建和删除的,关于该文件是否是新的,它会向寻呼机发送警报。因此,我必须找到一种方法来检查文件是否是新文件,否则寻呼机总是会响:p

代码语言:javascript
复制
std::map<std::string, std::string> pictures;

HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA ffd;

hFind = FindFirstFile(TEXT("C:\\temp\\*"), &ffd);
do
{
    Sleep(1000);
    bool isDirectory = ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
    if(isDirectory)
    {
        _tprintf(TEXT("%s\n"),ffd.cFileName);

    }
    else
    {
        //here is where I want to store the cFilename and ftCreationTime in the map
        //something strange here
        //tprintf returns the good filename but cout returns a character string like 0019FB2C for every file found
        _tprintf(TEXT("%s\n"),ffd.cFileName );
        std::cout << "FileTime: " << ffd.cFileName << std::endl;
    }
}while(FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-23 18:08:19

下面是一个简单的例子,说明您如何才能做到这一点。

我用了一个向量,但是你可以根据你的需要调整它。

请注意,这还没有经过测试或调试。

我还在一个.cpp文件中完成了所有这些工作;您可能应该将其分解。

首先,创建一个图片结构:

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#include <tchar.h>
#include <Windows.h>

typedef std::basic_string <TCHAR> tstring ;
typedef std::basic_ostream <TCHAR> tstream ;

#ifdef _UNICODE
#define tcout std::wcout
#else
#define tcout std::cout
#endif

struct Picture
{
    Picture (const tstring &name, const FILETIME &ft) ;

    tstring name ;
    FILETIME creation_time ;

    friend bool operator== (const Picture &lhs, const Picture &rhs) ;
    friend bool operator!= (const Picture &lhs, const Picture &rhs) ;
    friend tstream& operator<< (tstream& ts, const Picture &pic) ;
};

Picture::Picture (const tstring &name, const FILETIME &ft) : name (name), creation_time (ft)
{
}

bool operator== (const Picture &lhs, const Picture &rhs)
{
    return ((lhs.name == rhs.name) && (::CompareFileTime (&lhs.creation_time, &rhs.creation_time) == 0)) ;
}

bool operator!= (const Picture &lhs, const Picture &rhs)
{
    return !(operator== (lhs, rhs)) ;
}

tstream& operator<< (tstream& ts, const Picture &pic)
{
    ts  << pic.name << _T (", FileTime (HI, LO): (") 
        << pic.creation_time.dwHighDateTime << _T (", ")
        << pic.creation_time.dwLowDateTime << _T (")") ;
    return ts ;
}

然后实现一个输出新文件的函数。

代码语言:javascript
复制
void PrintNewPictures (std::vector <Picture> &vecPicsOld, const tstring &dir)
{
    HANDLE hFind = INVALID_HANDLE_VALUE;
    WIN32_FIND_DATA ffd ;

    std::vector <Picture> vecPics ;

    hFind = FindFirstFile(dir.data (), &ffd) ;
    if (hFind == INVALID_HANDLE_VALUE) {
        // Return an error or throw an exception
        return ;
    }

    do {
        Picture pic (ffd.cFileName, ffd.ftCreationTime) ;
        if (std::find (vecPicsOld.begin (), vecPicsOld.end (), pic) == vecPicsOld.end ()) {
            // Print that this is a new Picture.
            tcout << pic << std::endl ;
        }

        vecPics.push_back (pic) ;

    } while (::FindNextFile (hFind, &ffd) != NULL) ;
    ::FindClose (hFind) ;

    // This keeps the vector fresh so it won't build up old values.
    std::swap (vecPics, vecPicsOld) ;
}

下面是如何使用这个的示例:

代码语言:javascript
复制
int main (void) 
{
    std::vector <Picture> vecPics ;
    while (1) {
        ::Sleep (1000) ;
        PrintNewPictures (vecPics, _T ("C:\\temp\\*")) ;
    }

    return 0 ;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21314348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档