#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;
}