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;
}

执行的时候会报错 请大神指点一下那个地方有问题
...全文
254 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类型的地址上了
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随与博主沟通,第一进行解答!

64,651

社区成员

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

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