(linux系统)时间戳函数example

it2024-03-14  73

函数说明

1.time():get time in seconds from 1970-01-01 00:00:00 +0000 (UTC). 原型: time_t time(time_t *tloc); 2.localtime():transform date and time to broken-down time or ASCII 原型: **struct tm localtime(const time_t timep); 3.strftime() :format date and time 原型: **size_t strftime(char *s, size_t max, const char format,const struct tm tm);

示例

#include<stdio.h> #include<stdlib.h> #include<time.h> #include<unistd.h> #define FNAME "/tmp/out" #define BUFFSIZE 1024 int main() { struct tm *tm; FILE *fp; char buf[BUFFSIZE]; int count=0; time_t stamp; //追加读写a+ fp=fopen(FNAME,"a+"); if(fp==NULL) { perror("fopen()"); exit(1); } while(fgets(buf,BUFFSIZE,fp)!=NULL) { count++; } while(1) { time(&stamp); tm=localtime(&stamp); fprintf(fp,"%-4d%d-%d-%d %d:%d:%d\n",++count,\ tm->tm_year+1900,tm->tm_mon,tm->tm_mday,\ tm->tm_hour,tm->tm_min,tm->tm_sec); //刷新缓冲区:很重要 fflush(fp); sleep(1); } fclose(fp); exit(0); }
最新回复(0)