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

执行的时候会报错 请大神指点一下那个地方有问题
...全文
289 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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类型的地址上了

65,186

社区成员

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

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