• 将时间打印到一个文件并打印行号


    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <string.h>
    #include <unistd.h>
    #include <time.h>
    #include <stdlib.h>
    
    int get_line()
    {
        int line = 1;
        FILE *file1 = fopen("./time1.txt", "r");
        char *buf = calloc(32, sizeof(char));;
        while(fgets(buf, 32, file1) != NULL)
        {
            if (buf[strlen(buf)-1] == '
    ')
                 line++;
        }
        printf("%d
    ", line);
        return line;
    }
    
    int main(int argc, char const *argv[])
    {
        time_t timep;
       
        char *buf;
        char *file_name = "./time1.txt";
        int fd = open(file_name, O_WRONLY | O_APPEND );
        int i = get_line();
    
        while(1)
        {
            time (&timep);
            char tmp[32];
    
            buf = asctime(gmtime(&timep));
            sprintf(tmp,"[%d]%s",i, buf);
            sleep(1);
            int ret = write(fd, tmp, strlen(tmp));
            printf("ret:%d
    ", ret);
            if (ret == -1)
            {
               printf("write %s error , msg: %s
    " , file_name, strerror(errno));
            }
            fsync(fd);
            memset(tmp, '', 32);
            i++;
        }
        return 0;
    }
    

  • 相关阅读:
    Java并发编程
    Git
    Spring Boot
    IDEA工具
    Java基础
    数据库架构
    设计模式
    网络基础
    管理知识
    linux安装数据库mysql
  • 原文地址:https://www.cnblogs.com/ding-ding-light/p/14152275.html
Copyright © 2020-2023  润新知