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

本文介绍了两个C/C++函数,stringGetModifyTime用于从文件获取当前修改时间并格式化为字符串,SetModifyTime则接受时间字符串并设置文件的修改时间。这些函数适用于处理文件的元数据操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/gongxie0235/article/details/105244906

https://blog.youkuaiyun.com/evea804/article/details/32139941

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值