(linux系统)时间戳函数example

函数说明

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值