⑴ 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 */
……