- 帖子
- 209
- 积分
- 550
- 威望
- 886
- 金钱
- 697
- 在线时间
- 1 小时
|
本帖最后由 saitojie 于 2010-5-24 09:11 编辑
[讨论]请教一个C++新手的问题
信息来源:邪恶八进制信息安全团队(www.eviloctal.com)
议题作者:--------
文章备注:修改zhouzhen[E.S.T]发布的一个程序,希望实现修改各时间项.
虽有想改之心,却没实力,现在将代码贴出来,希望得到前辈的帮助.
我改来改去,总有错,而且也没达到目的.
#include <stdafx.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
void usage()
{
system("cls.exe");
printf("-------------------------------------------------------\n"
"\tsetTime Ver 1.0 zhouzhen[E.S.T], 2005/3/5\n"
"\thttp://www.eviloctal.com, zhouzhen@yeah.net\n"
"\t 文件时间修改器 1.0"
"\n\n"
"\tUsage: \tsetTime File CreationTime LastWriteTime LastAccessTime\n"
"\tExample: \tsetTime hello.asp 20080101181212 1 1\n"
"\t非时间格式则不修改那一项\n"
"--------------------------------------------------------\n"
);
exit(0);
}
void atotime(char* timestr,FILETIME ftime)
{
if(strlen(timestr)!=14)
return ;
char yy[4]={0},mm[2]={0},dd[2]={0},ho[2]={0},mi[2]={0},se[2]={0};//年月日时分秒
yy[0]=timestr[0];yy[1]=timestr[1];yy[2]=timestr[2];yy[3]=timestr[3];yy[4]='\0';
mm[0]=timestr[4];mm[1]=timestr[5];mm[2]='\0';
dd[0]=timestr[6];dd[1]=timestr[7];dd[2]='\0';
ho[0]=timestr[8];ho[1]=timestr[9];ho[2]='\0';
mi[0]=timestr[10];mi[1]=timestr[11];mi[2]='\0';
se[0]=timestr[12];se[1]=timestr[13];se[2]='\0';
printf("\ntimestr[0]---- %c ----\n",timestr[0]);
printf("\n---- %s ----\n",yy);
printf("\n---- %s ----\n",mm);
printf("\n---- %s ----\n",dd);
printf("\n---- %s ----\n",ho);
printf("\n---- %s ----\n",mi);
printf("\n---- %s ----\n",se);
SYSTEMTIME sysTime;
sysTime.wYear =atoi(yy);
sysTime.wMonth =atoi(mm);
sysTime.wDay =atoi(dd);
sysTime.wHour =atoi(ho);
sysTime.wMinute =atoi(mi);
sysTime.wSecond =atoi(se);
SystemTimeToFileTime(&sysTime, &ftime);
}
int main(int argc, char* argv[])
{
HANDLE hFileNew1 , hFileNew;
FILETIME OCreateTime, OLastWriteTime, OLastAccessTime;
const FILETIME *pCreationTime, *pLastWriteTime, *pLastAccessTime;
if (argc!=5)
usage();
else{
hFileNew1 = CreateFile(argv[1], GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFileNew1 == INVALID_HANDLE_VALUE)
{
printf("Cannot open %s. Error:%x\n", argv[1], GetLastError());
exit(2);
}
GetFileTime(hFileNew1 , &OCreateTime, &OLastAccessTime, &OLastWriteTime);
CloseHandle(hFileNew1);
hFileNew = CreateFile(argv[1], GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFileNew == INVALID_HANDLE_VALUE)
{
printf("Cannot open %s. Error:%x\n", argv[1], GetLastError());
exit(2);
}
atotime(argv[2],OCreateTime);
pCreationTime = &OCreateTime;
atotime(argv[3],OLastWriteTime);
pLastWriteTime = &OLastWriteTime;
atotime(argv[4],OLastAccessTime);
pLastAccessTime = &OLastAccessTime;
if(!SetFileTime(hFileNew, pCreationTime,pLastAccessTime,pLastWriteTime))
{
printf("Error SetFileTime\n");
exit(3);
}
CloseHandle(hFileNew);
printf("All is done! Enjoy yourself\n");
}
return 0;
} |
|