Format log file

Experimental content

Program to read and write a file test.txt, and write a line of data to the file every 1 second, similar to this

1, 2021-7-30 15:16:42

2, 2021-7-30 15:16:43

The program should loop indefinitely until you press Ctrl-C to interrupt the program. The next time you start the program to write a file, it can be appended to the original file, and the serial number can continue the last serial number, such as:

1, 2021-7-30 15:16:42

2, 2021-7-30 15:16:43

3, 2021-7-30 15:19:02

4, 2021-7-30 15:19:03

5, 2021-7-30 15:19:0

Experimental platform

PC, ubuntu operating system, gcc and other tools

Experimental tips

  • First, judge whether the open file is a new file. If it is a new file, write it from serial number 1; If not
    If it is a new file, count the original number of lines, such as N lines, and then write from sequence number n+1. Every time I write
    OK, just add 1 to the line number.
  • To get the current system time, you need to call the function time(). The result is a time_ Type T is actually
    A large integer whose value represents the time from 00:00:00 UTC time on January 1, 1970 (called UNIX Epoch time)
    (interval) the number of seconds to the current time. Then call localtime() to time_. The UTC time represented by T is converted to
    Local time (we are in + 8 zone, which is 8 hours more than UTC) and converted to struct tm type, and the numbers of this type
    According to the members, please write your own code to convert the format. Do not use ctime() or
    Actime() function. Please refer to man page for specific usage. The time and localtime functions require the header file time.h.
  • Calling sleep(n) can make the program sleep for N seconds. This function requires the header file unistd.h.

Experimental ideas

First kind

The first idea is to calculate the number of rows according to the above tips, and then increase the output at one time:

Code:

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

int cal_line(FILE *fp) {
    int a[7];
    int lines = 0;
    while(fscanf(fp,"%d, %d-%d-%d %02d:%02d:%02d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]) != EOF){
        lines++;
    }
    return lines;
}

int main() {

    char *path = "a.txt";
    FILE* fp_open = fopen(path,"a+");
    if(fp_open == NULL) {
        fclose(fp_open);
        perror("open_errot:");
        return -1;
    }
    int lines = cal_line(fp_open);
    fclose(fp_open);
    time_t now ;
    struct tm *tm_now ;
    while(1) {
        FILE* fp_write = fopen(path,"a");
        time(&now);
        tm_now = localtime(&now) ;//get date
        printf("%d, %d-%d-%d %02d:%02d:%02d\n",lines,tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
        fprintf(fp_write,"%d, %d-%d-%d %02d:%02d:%02d\n",lines++,tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
        fclose(fp_write);
        sleep(1);
    }
    return 0;
}

Second

In fact, it takes a lot of time to calculate the number of rows every time. Therefore, we can save the current number of rows every time we operate. We can call this file configuration file c.txt and log file b.txt, so that we can directly read the number of rows every time without counting

Code:

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

int main()
{
    char *path_log = "b.txt";//log file
    char *path_init = "c.txt";//configuration file
    FILE *fp_open = fopen(path_init,"a+");
    int lines = 0;
    if(fscanf(fp_open,"%d",&lines)==EOF) {
        lines = 0;
    }
    fclose(fp_open);
    time_t now ;
    struct tm *tm_now ;
    while(1) {
        time(&now);
        tm_now = localtime(&now) ;//get date
        FILE *fp_write = fopen(path_log,"a+");
        FILE *fp_init = fopen(path_init,"w");
        printf("%d, %d-%d-%d %02d:%02d:%02d\n",lines,tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
        fprintf(fp_write,"%d, %d-%d-%d %02d:%02d:%02d\n",lines++,tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
        fprintf(fp_init,"%d\n",lines);
        fclose(fp_write);
        fclose(fp_init);
        sleep(1);
    }
    return 0;
}

Keywords: C C++ Qt

Added by agentsmith on Tue, 12 Oct 2021 05:55:03 +0300