C/C++ SetFileTime 修改文件时间时报错

好男人一样 2016-07-27 11:24:49
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <io.h>
#include <windows.h>
#include <direct.h>

int _tmain(int argc, _TCHAR* argv[])
{

HANDLE hFile = CreateFileA("D:\\log.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);

//获取文件时间
FILETIME createTime,accessTime,writeTime;
GetFileTime(hFile,&createTime,&accessTime,&writeTime);

//将文件时间转换为系统时间
SYSTEMTIME sysTime;
FileTimeToSystemTime(&writeTime,&sysTime);

//格式化字符串
char msg[256];
sprintf_s(msg,"%d-%d-%d-%d-%d-%d-%d-%d",
sysTime.wYear,sysTime.wMonth,sysTime.wDay,sysTime.wDayOfWeek,sysTime.wHour,sysTime.wMinute,sysTime.wSecond,sysTime.wMilliseconds);

//将字符串转换为系统时间
SYSTEMTIME newSysTime;
sscanf_s(msg,"%d-%d-%d-%d-%d-%d-%d-%d",
&newSysTime.wYear,&newSysTime.wMonth,&newSysTime.wDay,&newSysTime.wDayOfWeek,&newSysTime.wHour,&newSysTime.wMinute,&newSysTime.wSecond,&newSysTime.wMilliseconds);

//将系统时间转换为文件时间
FILETIME newWriteTime;
SystemTimeToFileTime(&newSysTime,&newWriteTime);

//设置文件的时间
SetFileTime(hFile,&newWriteTime,&newWriteTime,&newWriteTime);

CloseHandle(hFile);

return 0;
}

执行的时候会报错 请大神指点一下那个地方有问题
...全文
307 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-07-28
  • 打赏
  • 举报
回复
仅供参考:
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <afxdisp.h>
#include <winbase.h>
COleDateTime dt;
char opt[5];//"/WAC"
int ArgNum;
int offset=1;
char *s;
char path[MAX_PATH];
char pathlnk[MAX_PATH+4];//.lnk
char tmp[20];//YYYY-MM-DD hh:mm:ss
HANDLE hfile;
BY_HANDLE_FILE_INFORMATION bhfi;
FILETIME ft;
SYSTEMTIME st;
int isGet=0;
int main(int argc,char **argv) {
    setlocale(LC_ALL,"chs");
    strcpy(opt,"/W");
    dt=COleDateTime::GetCurrentTime();
    if (argc==1) {
    HELP:
        printf("File or directory Datetime(WAC:lastWrite|lastAccess|Creation)\n");
        printf("Set to Now or YY-MM-DD [hh:mm:ss]    or    Get\n");
        printf("  FD [/WAC] File_or_Dir_name [[[20]YY-]MM-DD[ hh:mm:ss]]|get\n");
        return 1;
    }
    for (ArgNum=1;ArgNum<argc;ArgNum++) {
        if (ArgNum==0+offset) {
            s=argv[ArgNum];
            if (0==strcmp(s,"/?")) goto HELP;
            if ('/'==s[0]) {
                strncpy(opt,s,4);opt[4]=0;
                strupr(opt);
                if (NULL==strchr(opt,'A')&&NULL==strchr(opt,'C')&&strlen(opt)<4) strcat(opt,"W");
                offset=2;
            } else {
                strncpy(path,s,MAX_PATH-1);
                path[MAX_PATH-1]=0;
            }
        }
        if (ArgNum==1+offset) {
            s=argv[ArgNum];
            if (0==stricmp(s,"get")) isGet=1;
            else {
                if (!dt.ParseDateTime(s)) {
                    printf("Error: Invalid datetime format\n");
                    return 2;
                }
            }
        }
        if (ArgNum==2+offset) {
            strncpy(tmp,argv[ArgNum-1],10);tmp[10]=0;//YYYY-MM-DD
            strcat(tmp," ");
            strncat(tmp,argv[ArgNum],8);//hh:mm:ss
            if (!dt.ParseDateTime(tmp)) {
                printf("Error: Invalid datetime format\n");
                return 2;
			} else break;//
		}
	}
	if (isGet) hfile = CreateFile(path,	GENERIC_READ			  ,	FILE_SHARE_READ					, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	else	   hfile = CreateFile(path,	GENERIC_READ|GENERIC_WRITE,	FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (INVALID_HANDLE_VALUE!=hfile) {
		if (!GetFileTime(hfile,	&bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
			CloseHandle(hfile);
			printf("Error: GetFileTime!\n");
			return 4;
		}
		if (strchr(opt,'W')) {
			if (isGet) {
				dt=COleDateTime::COleDateTime(bhfi.ftLastWriteTime);
				printf("File Datetime(LastWrite ) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
			} else {
				dt.GetAsSystemTime(st);
				SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftLastWriteTime);
				if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
					printf("File Datetime(LastWrite ) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
				} else {
					CloseHandle(hfile);
					printf("Error: SetFileTime!\n");
					return 5;
				}
			}
		}
		if (strchr(opt,'C')) {
			if (isGet) {
				dt=COleDateTime::COleDateTime(bhfi.ftCreationTime);
				printf("File Datetime(Creation  ) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
			} else {
				dt.GetAsSystemTime(st);
				SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftCreationTime);
				if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
					printf("File Datetime(Creation  ) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
				} else {
					CloseHandle(hfile);
					printf("Error: SetFileTime!\n");
					return 5;
				}
			}
		}
		if (strchr(opt,'A')) {
			if (isGet) {
				dt=COleDateTime::COleDateTime(bhfi.ftLastAccessTime);
				printf("File Datetime(LastAccess) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
			} else {
				dt.GetAsSystemTime(st);
				SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftLastAccessTime);
				if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
					printf("File Datetime(LastAccess) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
				} else {
					CloseHandle(hfile);
					printf("Error: SetFileTime!\n");
					return 5;
				}
			}
		}
		CloseHandle(hfile);
		return 0;
	}
	strcpy(pathlnk,path);
	strcat(pathlnk,".lnk");
	if (isGet) hfile = CreateFile(pathlnk, GENERIC_READ				 , FILE_SHARE_READ				   , NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,	NULL);
	else	   hfile = CreateFile(pathlnk, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,	NULL);
	if (INVALID_HANDLE_VALUE!=hfile) {
		if (!GetFileTime(hfile,	&bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
			CloseHandle(hfile);
			printf("Error: GetFileTime!\n");
			return 4;
		}
		if (strchr(opt,'W')) {
			if (isGet) {
				dt=COleDateTime::COleDateTime(bhfi.ftLastWriteTime);
				printf("File.lnk Datetime(LastWrite ) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
			} else {
				dt.GetAsSystemTime(st);
				SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftLastWriteTime);
				if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
					printf("File.lnk Datetime(LastWrite ) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
				} else {
					CloseHandle(hfile);
					printf("Error: SetFileTime!\n");
					return 5;
				}
			}
		}
		if (strchr(opt,'C')) {
			if (isGet) {
				dt=COleDateTime::COleDateTime(bhfi.ftCreationTime);
				printf("File.lnk Datetime(Creation  ) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
			} else {
				dt.GetAsSystemTime(st);
				SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftCreationTime);
				if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
					printf("File.lnk Datetime(Creation  ) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
				} else {
					CloseHandle(hfile);
					printf("Error: SetFileTime!\n");
					return 5;
				}
			}
		}
		if (strchr(opt,'A')) {
			if (isGet) {
				dt=COleDateTime::COleDateTime(bhfi.ftLastAccessTime);
				printf("File.lnk Datetime(LastAccess) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
			} else {
				dt.GetAsSystemTime(st);
				SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftLastAccessTime);
				if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
					printf("File.lnk Datetime(LastAccess) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
				} else {
					CloseHandle(hfile);
					printf("Error: SetFileTime!\n");
					return 5;
				}
			}
		}
		CloseHandle(hfile);
		return 0;
	}
	if (isGet) hfile = CreateFile(path,	GENERIC_READ			  ,	FILE_SHARE_READ					, NULL,OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
	else	   hfile = CreateFile(path,	GENERIC_READ|GENERIC_WRITE,	FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
	if (INVALID_HANDLE_VALUE!=hfile) {
		if (!GetFileTime(hfile,	&bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
			CloseHandle(hfile);
			printf("Error: GetFileTime!\n");
			return 4;
		}
		if (strchr(opt,'W')) {
			if (isGet) {
				dt=COleDateTime::COleDateTime(bhfi.ftLastWriteTime);
				printf("Directory Datetime(LastWrite ) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
			} else {
				dt.GetAsSystemTime(st);
				SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftLastWriteTime);
				if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
					printf("Directory Datetime(LastWrite ) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
				} else {
					CloseHandle(hfile);
					printf("Error: SetFileTime!\n");
					return 5;
				}
			}
		}
		if (strchr(opt,'C')) {
			if (isGet) {
				dt=COleDateTime::COleDateTime(bhfi.ftCreationTime);
				printf("Directory Datetime(Creation  ) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
			} else {
				dt.GetAsSystemTime(st);
				SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftCreationTime);
				if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
					printf("Directory Datetime(Creation  ) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
				} else {
					CloseHandle(hfile);
					printf("Error: SetFileTime!\n");
					return 5;
				}
			}
		}
		if (strchr(opt,'A')) {
			if (isGet) {
				dt=COleDateTime::COleDateTime(bhfi.ftLastAccessTime);
				printf("Directory Datetime(LastAccess) of [%s] is %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
			} else {
				dt.GetAsSystemTime(st);
				SystemTimeToFileTime(&st,&ft);LocalFileTimeToFileTime(&ft,&bhfi.ftLastAccessTime);
				if (SetFileTime(hfile, &bhfi.ftCreationTime, &bhfi.ftLastAccessTime, &bhfi.ftLastWriteTime)) {
					printf("Directory Datetime(LastAccess) of [%s] set to %s\n",path,dt.Format("%Y-%m-%d %H:%M:%S"));
				} else {
					CloseHandle(hfile);
					printf("Error: SetFileTime!\n");
					return 5;
				}
			}
		}
		CloseHandle(hfile);
		return 0;
	}
	printf("Error: Can not find or open File or File.lnk or Dir [%s]\n",path);
	return 3;
}
paschen 版主 2016-07-28
  • 打赏
  • 举报
回复
sscanf_s的时候你是将一个int数据写到了一个WORD类型的地址上了
内容概要:本文针对当前车辆诊断系统中存在的诊断需求不统一、DID&DTC分配混乱、刷新流程不规范、沟通效率低及数据分散等问题,提出通过制定统一的诊断刷新规范并构建诊断需求管理平台的解决方案。方案旨在建立标准化的诊断框架与需求模板,开发集需求管理、数据分配、流程监控于一体的统一平台,并推行规范化诊断刷新流程,从而实现需求变更减少80%、设计冲突排查降低90%、沟通协调减少50%等目标,最终提升研发效率、降低研发与售后成本、缩短产品上市周期。; 适合人群:汽车电子系统工程师、诊断开发人员、ECU供应商技术人员、项目管理人员及企业战略决策者;适用于从事车辆诊断系统设计、开发、测试及相关管理工作的专业人员。; 使用场景及目标:①用于指导企业建立统一的车辆诊断规范体系与数字化管理平台;②解决多供应商环境下诊断数据冲突与集成难题;③优化诊断刷新流程以提升安全性与效率;④降低整车研发成本并加速产品上市进程; 阅读建议:此文档兼具技术方案与商业价值分析,建议结合实施计划与预算章节进行全流程理解,重点关注规范制定、平台功能设计与跨团队协同机制,在实际项目中分阶段推进试点与落地。
【论文复现】一种基于价格弹性矩阵的居民峰谷分电价激励策略【需求响应】(Matlab代码实现)内容概要:本文介绍了一种基于价格弹性矩阵的居民峰谷分电价激励策略,旨在通过需求响应机制优化电力系统负荷分布,提升电网运行效率。该研究利用Matlab进行仿真与代码实现,构建价格弹性模型,分析居民用户在不同电价段下的用电行为响应,进而制定合理的峰谷电价方案,引导用户错峰用电,缓解高峰负荷压力。文中详细阐述了模型构建过程、算法实现逻辑及仿真结果分析,验证了该策略在促进负荷均衡和提升电力系统经济性方面的有效性。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的高校学生、科研人员及从事需求响应、能源管理等相关领域的工程技术人员。; 使用场景及目标:①研究居民用电行为与电价之间的响应关系;②设计并仿真基于价格弹性的分电价激励策略;③实现需求响应在电力系统优化调度中的具体应用;④为电力部门制定科学合理的电价政策提供技术支持与决策参考。; 阅读建议:建议读者结合Matlab代码逐段理解模型实现细节,重点关注价格弹性矩阵的构建方法与需求响应模型的仿真流程,同可参照文中案例进行复现实验,加深对理论与算法应用的理解。
【复现】基于改进秃鹰算法的微电网群经济优化调度研究(Matlab代码实现)内容概要:本文围绕“基于改进秃鹰算法的微电网群经济优化调度研究”展开,重点介绍了利用改进秃鹰算法(Bald Eagle Search Algorithm, BES)对微电网群进行经济优化调度的Matlab代码实现方法。该研究旨在解决微电网系统中多能源协调、运行成本最小化与供电可靠性之间的平衡问题,通过引入算法改进策略提升寻优能力与收敛速度,有效应对微电网中可再生能源出力波动性和负荷不确定性带来的调度挑战。文中详细阐述了微电网群的系统架构、目标函数构建、约束条件设定以及改进算法的应用流程,并通过仿真实验验证了其在降低运行成本、提高能源利用效率方面的优越性。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的高校研究生、科研人员及从事微电网、智能电网优化调度相关工作的工程技术人员,尤其适合正在开展能源优化、智能算法应用研究的研究者。; 使用场景及目标:①用于微电网群多目标经济调度模型的构建与求解;②作为智能优化算法(如秃鹰算法)改进与应用的学习案例;③支撑科研论文复现、算法对比实验及实际项目中的能源管理系统开发。; 阅读建议:建议结合提供的Matlab代码进行同步调试与仿真,重点关注目标函数设计、约束处理方式与算法改进机制,同可将其与其他智能算法(如粒子群、遗传算法)进行对比分析,深化对优化调度问题求解路径的理解。

65,206

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧