mktime()返回-1

dhusniper 2013-02-21 03:33:01
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <io.h>
#include "sys\stat.h"
#include "memory.h"
#include <conio.h>

typedef struct date
{
int dwYear;
int dwMonth;
int dwDay;
int dwHour;
int dwMin;
int dwSec;
} tDate;

void main()
{
char aszFileName[50];
struct stat FileInfo;
struct tm TempTime;
struct tm* pTempTime;
tDate date;
time_t dwFileTime;
time_t dwTempTime;
char aszBufTime[65];

memset(&TempTime,0,sizeof(tm));

FILE *fpFileList,*fpFileTime,*fpReport;

if((fpFileList=fopen("c:\\FileList.txt","r"))==NULL)
{
printf("打开文件名称列表失败!");
}

if((access("c:\\FileTime.txt",0))==-1)
{
if((fpFileTime=fopen("c:\\FileTime.txt","w"))==NULL)
{
printf("打开文件信息列表失败!");
}

while(!feof(fpFileList))
{
fscanf(fpFileList,"%s",aszFileName);
printf("%s\n",aszFileName);

stat(aszFileName,&FileInfo);
printf("%d\n",FileInfo.st_mtime);
fprintf(fpFileTime,"%d ",FileInfo.st_mtime);

}
fclose(fpFileTime);
}
else
{
rewind(fpFileList);
if((fpFileTime=fopen("c:\\FileTime.txt","r"))==NULL)
{
printf("打开文件信息列表失败!");
}

if((fpReport=fopen("c:\\Report.txt","w"))==NULL)
{
printf("打开输出报告文件失败!");
}

printf("输入时间基准: ");
scanf("%d-%d-%d %d:%d:%d",&date.dwYear,&date.dwMonth,&date.dwDay,&date.dwHour,&date.dwMin,&date.dwSec);

TempTime.tm_year=date.dwYear-1990;
TempTime.tm_mon=date.dwMonth-1;
TempTime.tm_mday=date.dwDay;
TempTime.tm_hour=date.dwHour;
TempTime.tm_min=date.dwMin;
TempTime.tm_sec=date.dwSec;
TempTime.tm_isdst=0;

printf("%d-%d-%d %d:%d:%d\n",TempTime.tm_year,TempTime.tm_mon,TempTime.tm_mday,TempTime.tm_hour,TempTime.tm_min,TempTime.tm_sec);

dwTempTime=mktime(&TempTime);
printf("%d\n",dwTempTime);

while((!feof(fpFileList))&&(!feof(fpFileTime)))
{
fscanf(fpFileList,"%s",aszFileName);
stat(aszFileName,&FileInfo);
fscanf(fpFileTime,"%d",&dwFileTime);

if(dwFileTime!=FileInfo.st_mtime)
{
pTempTime=localtime(&FileInfo.st_mtime);
sprintf(aszBufTime,"%04d-%02d-%02d %02d:%02d:%02d",pTempTime->tm_year+1900,pTempTime->tm_mon+1,pTempTime->tm_mday,pTempTime->tm_hour,pTempTime->tm_min,pTempTime->tm_sec);
fprintf(fpReport,"%s %s\n",aszFileName,aszBufTime);
}

}
fclose(fpFileTime);
fclose(fpReport);
}
fclose(fpFileList);
system("pause");



}
...全文
286 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2013-02-21
  • 打赏
  • 举报
回复
引用 2 楼 aa122333 的回复:
扫了一下,“TempTime.tm_year=date.dwYear-1990;” 这里应该是减去 1900,不知1990和输入数据是不是造成你-1结果
++ 一般建议先bzero或者memset一下。
aa122333 2013-02-21
  • 打赏
  • 举报
回复
扫了一下,“TempTime.tm_year=date.dwYear-1990;” 这里应该是减去 1900,不知1990和输入数据是不是造成你-1结果
dhusniper 2013-02-21
  • 打赏
  • 举报
回复
在mktime函数中,返回值出现-1,各位大虾请帮帮,在线等 ,很急,谢谢!

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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