gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:24:32 30 4
gpt4 key购买 nike

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

这段代码的最终目标是检测是否在目录中创建了一个具有相同内容的新文件。一些图片由软件定期创建和删除,并且关于文件是否是新的它向寻呼机发送警报。所以我必须找到一种方法来检查文件是否是新的,否则寻呼机会一直响: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);

最佳答案

这是一个简单的示例,说明您可以如何做到这一点。
我使用了 vector ,但您可以根据需要进行调整。
请注意,这尚未经过测试或调试。
我也在一个 .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 ;
}

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

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 ;
}

关于c++ - 将 FILETIME 转换为字符串,否则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21314348/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com