#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
int get_line(FILE *fp)
{
int line = 0;
char buf[1024];
//buf:....\n\0
while(fgets(buf,sizeof(buf),fp) != NULL)
{
if(buf[strlen(buf) - 1] == '\n')
line ++;
}
return line;
}
void do_log(FILE *fp)
{
time_t tim;
struct tm *ptm;
int line = get_line(fp);
while(1)
{
tim = time(NULL);
ptm = localtime(&tim);
fprintf(fp,"%d,%d-%d-%d %d:%d:%d.\n",++line,ptm->tm_year + 1900,ptm->tm_mon + 1,\
ptm->tm_mday,ptm->tm_hour,ptm->tm_min,ptm->tm_sec);
fflush(fp);
sleep(1);
printf("ok.\n");
}
return ;
}
//./a.out log.txt
int main(int argc, const char *argv[])
{
FILE *fp;
if(argc < 2)
{
fprintf(stderr,"Usage : %s argv[1].\n",argv[0]);
return -1;
}
if((fp = fopen(argv[1],"a+")) == NULL)
{
fprintf(stderr,"Fail to fopen %s : %s.\n",argv[1],strerror(errno));
return -1;
}
do_log(fp);
fclose(fp);
return 0;
}
记录时间日志
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
struct rtc_time{
int tm_sec; //当前秒
int tm_min; //当前分钟
int tm_hour; //当前小时
int tm_mday; //当前在本月中的天,如11月1日,则为1
int tm_mon; //当前月,范围是0~11
int tm_year; //当前年和1900的差值,如2006年则为36
};
int get_time()
{
struct tm *get_tm;
struct timeval get_tv;
gettimeofday(&get_tv, NULL);
get_tm = localtime(&get_tv.tv_sec);
#if 0
printf("%s \n", asctime(get_tm));
#else
printf("year %d mon %d mday %d hour %d min %d sec %d \n", get_tm->tm_year + 1900, get_tm->tm_mon + 1,
get_tm->tm_mday, get_tm->tm_hour,
get_tm->tm_min , get_tm->tm_sec);
#endif
return 0;
}
void split(char *src,const char *separator,char **dest,int *num)
{
/*
src 源字符串的首地址(buf的地址)
separator 指定的分割字符
dest 接收子字符串的数组
num 分割后子字符串的个数
*/
char *pNext;
int count = 0;
if (src == NULL || strlen(src) == 0) //如果传入的地址为空或长度为0,直接终止
return;
if (separator == NULL || strlen(separator) == 0) //如未指定分割的字符串,直接终止
return;
pNext = (char *)strtok(src,separator); //必须使用(char *)进行强制类型转换(虽然不写有的编译器中不会出现指针错误)
while(pNext != NULL) {
*dest++ = pNext;
++count;
pNext = (char *)strtok(NULL,separator); //必须使用(char *)进行强制类型转换
}
*num = count;
}
int time_service(char *buf)
{
printf("buf:%s\n", buf);
struct rtc_time tm;
struct tm _tm;
struct timeval tv;
time_t timep;
sscanf(buf, "%d:%d:%d:%d:%d:%d", &tm.tm_year,
&tm.tm_mon, &tm.tm_mday,&tm.tm_hour,
&tm.tm_min, &tm.tm_sec);
_tm.tm_sec = tm.tm_sec+180;
_tm.tm_min = tm.tm_min;
_tm.tm_hour = tm.tm_hour;
_tm.tm_mday = tm.tm_mday;
_tm.tm_mon = tm.tm_mon - 1;
_tm.tm_year = tm.tm_year - 1900;
printf("y:%d m:%d d:%d h:%d m:%d s:%d\n", _tm.tm_year, _tm.tm_mon, _tm.tm_mday, _tm.tm_hour, _tm.tm_min, _tm.tm_sec);
timep = mktime(&_tm);
tv.tv_sec = timep;
tv.tv_usec = 0;
if(settimeofday (&tv, (struct timezone *) 0) < 0)
{
printf("Set system datatime error!/n");
return -1;
}
else
{
printf("settimeofday ok...\n");
}
get_time();
return 0;
}
int get_line(FILE *fp, char *out_buf)
{
int line = 0;
char buf[1024];
int len = 0;
//buf:....\n\0
while(fgets(buf,sizeof(buf),fp) != NULL)
{
if(buf[strlen(buf) - 1] == '\n')
{
line ++;
}
}
len = strlen(buf);
int i = 0;
for(i = 0 ; i < len; i++)
{
printf("str=%c ",buf[i]);
if (buf[i] == ':')
{
printf("find it i:%d...\n",i);
memcpy(out_buf, buf+i+1, len-i-1);
break;
}
}
printf("out_buf=%s\n", out_buf );
return line;
}
void do_log(FILE *fp)
{
char out_buf[1024]={0};
time_t tim;
struct tm *ptm;
int line = get_line(fp, out_buf);
printf("out_buf:%s\n",out_buf);
time_service(out_buf);
//while(1)
{
tim = time(NULL);
ptm = localtime(&tim);
fprintf(fp,"%d:%d:%d:%d:%d:%d:%d\n",++line,ptm->tm_year + 1900,ptm->tm_mon + 1,\
ptm->tm_mday,ptm->tm_hour,ptm->tm_min,ptm->tm_sec);
fflush(fp);
usleep(10*100);
printf("ok.\n");
}
return ;
}
//./a.out log.txt
int main(int argc, const char *argv[])
{
get_time();
FILE *fp;
if(argc < 2)
{
fprintf(stderr,"Usage : %s argv[1].\n",argv[0]);
return -1;
}
if((fp = fopen(argv[1],"a+")) == NULL)
{
fprintf(stderr,"Fail to fopen %s : %s.\n",argv[1],strerror(errno));
return -1;
}
do_log(fp);
fclose(fp);
return 0;
}
2010

被折叠的 条评论
为什么被折叠?



