07.02 庫函數——ctime()和ctime不安全函數

聲明:char *ctime(const time_t *timer)

timer:這是指向time_t對象的指針,該對象包含一個日曆時間

#include<stdio.h>

#include<time.h>

int main(){

time_t curtime;//定義對象curtime;

time(&curtime);//用time函數將對象curtime轉換指針

printf("當前時間=%s",ctime(&curtime));//返回當前時間

return 0;

}

ctime不安全函數:

errno_t ctime_s(char *buffer,rsize_t bufsz,const time_t *time)

#include<stdio.h>

#include<time.h>

int main(){

time_t curtime=time(null);

char str[26];

ctime_s(str,sizeof str,&curtime);//對象,變量字節大小,指針對象。

printf(str);

}

int main(void) {

time_t now = time(0);

char str[26];

ctime_s(str, sizeof str, &now);

cout << str << endl;

cout << now << endl;

struct tm tml;

localtime_s(&tml,&now);

cout << "年:" << tml.tm_year+1900 << endl;

cout << "月:" << tml.tm_mon+1<< endl;

cout << "日:" << tml.tm_mday << endl;

cout << "時間:" << tml.tm_hour << ":" << tml.tm_min << ":" << tml.tm_sec << endl;

cout << tml.tm_wday << endl;

cout << tml.tm_yday+1 << endl;

system("pause");

return 0;

}


分享到:


相關文章: