欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c/c++获取和修改windows下文件修改时间

程序员文章站 2022-05-24 14:37:09
...
string GetModifyTime(const char* pstrFilename)
{
	struct _stat64i32 statbuf;
	_tstat(pstrFilename, &statbuf);
	tm tmCurTime;
	_localtime64_s(&tmCurTime, &(statbuf.st_mtime));
	char szRet[MAX_PATH];
	_stprintf(szRet, "%4d-%02d-%02d %02d:%02d:%02d", tmCurTime.tm_year+1900, tmCurTime.tm_mon+1, tmCurTime.tm_mday, tmCurTime.tm_hour, tmCurTime.tm_min, tmCurTime.tm_sec);
	return string(szRet);
}

void SetModifyTime(const char* pstrFilename, const char* pstrTime)
{
	if(pstrTime == NULL)
		return;
	struct tm tmCurTime;
	sscanf(pstrTime, "%4d-%02d-%02d %02d:%02d:%02d", &tmCurTime.tm_year, &tmCurTime.tm_mon, &tmCurTime.tm_mday, &tmCurTime.tm_hour, &tmCurTime.tm_min, &tmCurTime.tm_sec);
	tmCurTime.tm_year -= 1900;
	tmCurTime.tm_mon -= 1;
	time_t rawtime = mktime(&tmCurTime);
	_utimbuf mvTime;
	mvTime.actime = rawtime;
	mvTime.modtime =  rawtime;
	_utime(pstrFilename, &mvTime);
}

参考:https://blog.csdn.net/gongxie0235/article/details/105244906

https://blog.csdn.net/evea804/article/details/32139941