⑴ c语言如何实现日志文件里面只保留最近500条消息,超过的就自动删除掉
日志文件每日内容前加入时间戳
建立临时文件
从相应时间戳开始读取内容写入临时文件
删除原文件,临时文件更名
⑵ C语言如何获取本地时间,然后取时、分、秒的值
#include <stdio.h>
#include <time.h>
int main()
{time_t timep;
struct tm *tp;
time(&timep);
int p;
tp = localtime(&timep); //取得系统时间
printf("Today is %d-%d-%d ", (1900 + tp->tm_year), (1 + tp->tm_mon), tp->tm_mday);
printf("Now is %d:%02d:%02d ", tp->tm_hour, tp->tm_min, tp->tm_sec);
p=tp->tm_sec;
printf("p=%d ",p);
return 0;
}
⑶ 求LINUX下,C语言编写的日志输出源码~
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <time.h>
#define LOGFILE "./dir_log_0"
int g_Count;
//#define MAXLEN 1024
void WriteDebugLog(char *str);
int main(int argc, char **argv)
{
char str[1024]={0};
strcpy(str,"file no find");
int i=0,j=0;
for (i=0; i<10; i++)
{
for (j=0; j<50; j++)
{
WriteDebugLog(str);
}
}
return 0;
}
void WriteDebugLog(char *str)
{
char buf[2048]={0};
char logFileName[50]={0};
//long MAXLEN = 50*1024*1024;//50MB
int iMax = 1024;//1K
time_t timep;
FILE *fp = NULL;
struct tm *p;
time(&timep);
p = localtime(&timep);
memset(buf,0,sizeof(buf));
sprintf(buf,"[%d-%d-%d %d:%d:%d][DEBUG]",(1900+p->tm_year),(1+p->tm_mon), p->tm_mday,p->tm_hour, p->tm_min, p->tm_sec); //星期p->tm_wday
strcat(buf,str);
strcat(buf," ");
strcpy(logFileName,LOGFILE);
int len = strlen(logFileName);
logFileName[len-1] = Ɔ'+g_Count;
fp = fopen(logFileName,"r");
if(fp==NULL)
{
fp = fopen(logFileName,"w+");
}
else
{
fseek(fp,0,2);//SEEK_END值为2
if( ftell(fp) >= iMax)
{
fclose(fp);
if (g_Count >= 9)
{
logFileName[len-1] = Ɔ'
g_Count=0;
}
else
{
g_Count++;
logFileName[len-1] = Ɔ'+g_Count;
// printf(" %c",Ɔ'+g_Count);
}
fp = fopen(logFileName,"w+");
}
else
{
fclose(fp);
fp = fopen(logFileName,"a");
}
}
fwrite(buf,1,strlen(buf),fp);
fclose(fp);
}
⑷ 如何使用C语言程序获取指定日期的UNIX时间戳
C/C++ code #include #include int main(void) { time_t tick; struct tm tm; char s[100]; tick = time(NULL); tm = *localtime(&tick); strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm); printf("%d: %s\n", (int)tick, s); return 0; }
⑸ c语言写的socket程序客户端,给服务端发送消息。加上时间戳,就是在消息前面加上时间。
time_tt;
structtm*ti;
charbuffer[1048],input[1024];
time(&t);//获取时间戳
ti=localtime(&t);//转成详细时间信息
//格式化时间字符串
sprintf(buffer,"%04d/%02d/%02d%02d:%02d:%02d",
ti->tm_year+1900,
ti->tm_mon+1,
ti->tm_mday,
ti->tm_hour,
ti->tm_min,
ti->tm_sec);
fgets(input,1024,stdin);
strcat(buffer,input);//连接两个字符串
printf("%s",buffer);
send(sockfd,buffer,strlen(buffer),0);
//望采纳
⑹ C语言 验证时间戳
有,时间戳一般是一个毫秒值。有系统函数可以获取。具体是啥记不清了
⑺ 如何用C语言获取当前系统时间
需要利用C语言的时间函数time和localtime,具体说明如下:
一、函数接口介绍:
1、time函数。
形式为time_t time (time_t *__timer);
其中time_t为time.h定义的结构体,一般为长整型。
这个函数会获取当前时间,并返回。 如果参数__timer非空,会存储相同值到__timer指向的内存中。
time函数返回的为unix时间戳,即从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。
由于是秒作为单位的,所以这并不是习惯上的时间,要转为习惯上的年月日时间形式就需要另外一个函数了。
2、localtime函数。
形式为struct tm *localtime (const time_t *__timer);
其中tm为一个结构体,包含了年月日时分秒等信息。
这种结构是适合用来输出的。
二、参考代码:
#include<stdio.h>
#include<time.h>
intmain()
{
time_tt;
structtm*lt;
time(&t);//获取Unix时间戳。
lt=localtime(&t);//转为时间结构。
printf("%d/%d/%d%d:%d:%d ",lt->tm_year+1900,lt->tm_mon,lt->tm_mday,lt->tm_hour,lt->tm_min,lt->tm_sec);//输出结果
return0;
}
注意事项:
struct tm中的tm_year 值为实际年减去1900, 所以输出的时候要是lt->tm_year+1900。
⑻ C语言怎么获取时间戳
DWORD tm = ::GetTickCount();
……
#ifndef _TIME32_T_DEFINED
typedef _W64 long __time32_t; /* 32-bit time value */
#define _TIME32_T_DEFINED
#endif /* _TIME32_T_DEFINED */
#ifndef _TIME64_T_DEFINED
#if _INTEGRAL_MAX_BITS >= 64
typedef __int64 __time64_t; /* 64-bit time value */
#endif /* _INTEGRAL_MAX_BITS >= 64 */
#define _TIME64_T_DEFINED
#endif /* _TIME64_T_DEFINED */
#ifndef _TIME_T_DEFINED
#ifdef _USE_32BIT_TIME_T
typedef __time32_t time_t; /* time value */
#else /* _USE_32BIT_TIME_T */
typedef __time64_t time_t; /* time value */
#endif /* _USE_32BIT_TIME_T */
#define _TIME_T_DEFINED /* avoid multiple def's of time_t */
#endif /* _TIME_T_DEFINED */
……