本程序实现在txt文件记录最新的20条log记录,当txt文件中一开始没有20条记录时,逐条增加log记录,当文件中存在20条记录,此时又有记录需要写入文件时,删除最下方最老的一条记录,将最新的一条记录写入文件最顶部。
int WRITE_LOG(unsigned char *buffer_IP)
{
char arr[MAXLINE][MAXCOLUMN]={0};
int i=-1;
int j;
int k;
pFilePinter = fopen("/log.txt","r");
if (pFilePointer == NULL)
{
printf("open log file error!\n");
return -1
}
while(!feof(pFilePointer))
{
i++;
fgets(arr[i],MAXCOLUMN+1,pFilePointer);
}
fclose(pFilePointer);
pFilePointer = NULL;
fp = fopen("/log.txt","w");
if (fp == NULL)
{
printf("open log file error!\n");
return -1
}