C++ 将FILETIME转换为字符串,否则

C++ 将FILETIME转换为字符串,否则,c++,C++,我想存储目录中每个文件的文件名和创建时间。我发现下面的代码可以浏览目录并检测每个文件。问题是我不知道如何存储WIN32_FIND_数据中的值。cFilename不应该太难,它是TCHAR(但我是C++的初学者),ftCreationTime是一种结构,因此它不能存储到向量中,因为它没有构造函数 这段代码的最终目标是检测目录中是否创建了具有相同内容的新文件。一些图片是由软件定期创建和删除的,关于文件是否是新的,它会向寻呼机发送警报。所以我必须找到一种方法来检查文件是否是新的,否则寻呼机总是会响:p

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

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

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);
std::地图图片;
句柄hFind=无效的句柄值;
WIN32_查找_数据ffd;
hFind=FindFirstFile(文本(“C:\\temp\\*”),&ffd);
做
{
睡眠(1000);
bool isDirectory=ffd.dwFileAttributes&FILE\u ATTRIBUTE\u目录;
国际单项体育联合会(isDirectory)
{
_tprintf(文本(“%s\n”),ffd.cFileName);
}
其他的
{
//这里是我想在地图中存储cFilename和ftCreationTime的位置
//这里有点奇怪
//tprintf返回正确的文件名,但对于找到的每个文件,cout都会返回类似0019FB2C的字符串
_tprintf(文本(“%s\n”),ffd.cFileName);

std::cout这里有一个快速的例子,说明您可以如何做到这一点。
我使用了一个向量,但您可以根据需要调整它。
请注意,尚未对其进行测试或调试。
我也在一个.cpp文件中完成了这一切;您可能应该将其分解

首先,创建图片结构:

#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 ;
}
#包括
#包括
#包括
#包括
#包括
#包括
typedef std::基本字符串tstring;
typedef std::basic_ostream tstream;
#ifdef_UNICODE
#定义tcout std::wcout
#否则
#定义tcout std::cout
#恩迪夫
结构图
{
图片(常量字符串和名称、常量文件时间和ft);
t字符串名称;
文件创建时间;
friend bool运算符==(常量图片和左侧、常量图片和右侧);
friend bool运算符!=(常量图片和左侧、常量图片和右侧);

friend tstream&operator“ftCreationTime是一种结构,因此无法将其存储到向量中,因为它没有构造函数。”-当然,一个结构有一个构造函数,如果你没有定义构造函数,编译器将始终生成默认构造函数。如果你有unicode构建,那么你应该使用
std::wcout
,而不是
std::cout
。你可以使用宏将合适的构造函数定义为
tcout
。thokra:谢谢你的更正,我已经重新定义了广告基础上的C++,所以我犯了这个错误JLIF902:一个很大的感谢你的代码,我会尝试它;)“将文件时间转换为字符串,否则”否则呢?你会吃掉我们的孩子…胡萝卜,如果我们不想要的话?谢谢你的贴切评论,特别是当这个问题很久以前就解决了的时候;)非常感谢。我无法测试是因为:Picture::Picture(const tstring&name,const FILETIME&ft):name(name),creation_time(ft)Visual Studio不理解“:”(这是在.cpp中)。当我尝试在.h中声明它时,就像这样,它告诉我它需要一个“{”(作为一个好的构造函数),我只是喜欢你发现新图片的方式:if(std::find(vecpicsell.begin(),vecpicsell.end(),pic)=vecpicsell.end())。多么优雅的家伙!我在Visual Studio 2012中写了这个,但它在Visual Studio 2008中也应该可以使用。那
是a的开头。请注意它后面的括号。请确保在任何结构或类的末尾都没有留下分号。我支持2010。我在中看到了你的分号,并尝试将它们添加到函数中标题。如果它不起作用,我将尝试在函数中以其他方式声明图片。非常感谢您的jliv;)
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) ;
}
int main (void) 
{
    std::vector <Picture> vecPics ;
    while (1) {
        ::Sleep (1000) ;
        PrintNewPictures (vecPics, _T ("C:\\temp\\*")) ;
    }

    return 0 ;
}