gpt4 book ai didi

c++ - 将文件的最后修改日期转换为字符串 C++

转载 作者:行者123 更新时间:2023-11-28 02:37:49 26 4
gpt4 key购买 nike

我正在编写一个脚本,该脚本将从一周前修改过的文件夹中删除文件,但我无法将最后修改日期放入字符串中。

所以我试图将文件的最后修改日期写入字符串 vector 。

粗体是错误行,它说

||=== Build: Debug in WeekaDelete (compiler: GNU GCC Compiler) ===| \WeekaDelete\main.cpp||In function 'int main(int, char**)':| \WeekaDelete\main.cpp|21|error: cannot bind 'std::ostream {aka std::basic_ostream}' lvalue to 'std::basic_ostream&&'| codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream|602|error: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits; _Tp = _FILETIME]'| ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

#include <windows.h>
#include <vector>
#include <ctime>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(int argc, char* argv[])
{
WIN32_FIND_DATA search_data;
memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
HANDLE handle = FindFirstFile("C:\\Users\\Meikle-John\\Desktop\\CoastWideCivil\\C++\\Scans\\*", &search_data);
int ifilecount = -2;
vector<string> vsname, vsdate;
string tempn, tempd;
while(handle != INVALID_HANDLE_VALUE)
{
tempn = search_data.cFileName;
**tempd = search_data.ftLastAccessTime;**
cout << tempd << endl;
cout << tempn << " : " << tempd << endl;
cout << ifilecount++ << endl;
if(ifilecount > -1)
{
vsname.push_back(tempn);
vsdate.push_back(tempd);
}
if(FindNextFile(handle, &search_data) == FALSE)
{
break;
}
}
//Close the handle after use or memory/resource leak
FindClose(handle);
cout << "There are:" << ifilecount << " Files in this directory" << endl;
return 0;
}

最佳答案

由于您使用的是 Win32,最简单的方法是使用 GetDateFormat功能:

TCHAR tchDate[80];

SYSTEMTIME st;
FileTimeToSystemTime(&search_data.ftLastAccessTime, &st);

GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE,
&st, nullptr, tchDate, _countof(tchDate));
cout << tchDate;

还有GetTimeFormat如果您想要时间和日期。

关于c++ - 将文件的最后修改日期转换为字符串 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26942714/

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