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

  • Post author:
  • Post category:其他


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



版权声明:本文为dragoo1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。