localtime返回null。爲什麼? (我正在使用Visual C++ 2008)localtime返回null
struct tm *tb;
time_t lDate;
time(&lDate);
tb = localtime(&lDate); // tb is null everytime I try this!
localtime返回null。爲什麼? (我正在使用Visual C++ 2008)localtime返回null
struct tm *tb;
time_t lDate;
time(&lDate);
tb = localtime(&lDate); // tb is null everytime I try this!
是您的確切代碼?我剛剛編譯這個程序,它工作正常:
#include <stdio.h>
#include <time.h>
int main(int argc, char **argv)
{
struct tm *tb;
time_t lDate;
time(&lDate);
if (lDate == -1) {
perror("time");
return 1;
}
tb = localtime(&lDate);
if (tb == NULL) {
fprintf(stderr, "localtime failed\n");
return 1;
}
printf("Good\n");
return 0;
}
您在留言發佈的代碼工作正常,直到你得到的if語句。我不確定你在這裏做什麼,但你有;
在if (pArea);
幾乎肯定不應該在那裏(很難說,因爲它被格式化可怕,因爲你把它放在評論中)。你也一直在返回0,你打算怎麼做?
#include <time.h>
#include <stdio.h>
int main(void)
{
// get the current time
time_t now = time(0);
struct tm* theTime = localtime(&now);
int t=(int)theTime;
printf("%d",t);
getch();
return 0;
}
它的工作原理
您好,您可以提供整個代碼嗎?它適用於我的電腦上的gcc,我想測試你的代碼。 – 2011-03-28 12:58:26
你可以在調用'localtime'後查看'errno'並告訴我們它的價值是什麼? – Luke 2011-03-28 13:33:52
@Luke'localtime'不設置'errno',檢查手冊頁。 – Jonathan 2011-03-28 13:46:04