日志清空c语言版本

来自linux中国网wiki
跳到导航 跳到搜索

wipelog.c

// March   15  2022 

cat wipe.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#define PATHNAME "/home/evan/tmp/test"

int main(void)
{
   int fd;
   fd = open(PATHNAME,O_CREAT | O_WRONLY | O_TRUNC);
   ftruncate(fd,0);
   close(fd);   
   return 0;
   
}















#include<stdio.h>
#include <stdlib.h>

int main()
{
        FILE *fp;
        if ((fp=fopen("/opt/mycat/logs/wrapper.log","r")) == NULL)
        //if ((fp=fopen("./test.log","r")) != NULL)  // 如果是要清空 记得打开这行 注释上面行

        {
                printf("be  opened.\n");
		fp=fopen("/opt/mycat/logs/wrapper.log","w");
		fclose(fp);
                //exit(1);
                
        }
}