目录
1>写一个日志文件,将程序启动后,每一秒的时间写入到文件中
1、2024-7-29 10:31:19
2、2024-7-29 10:31:20
3、2024-7-29 10:31:21
:ctrl+c:停止程序
/a.out
4、2024-7-29 10:35:06
5、2024-7-29 10:35:07
6、2024-7-29 10:35:08
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int read_count();
void write_count(int count);
int main(int argc, char const *argv[])
{
// 定义变量存储秒数
time_t new_time;
time_t last_time = 0;
// 存放转变后的字符串
char buf[128] = "";
// 序号变量,从文件读取
int count = read_count();
while(1)
{
new_time = time(NULL);
if(new_time != last_time)
{
// 将秒数转换为结构体
struct tm *time_ptr = localtime(&new_time);
// 把当前时间转换成字符串放入字符数组中,并添加序号
sprintf(buf, "%d. %4d-%02d-%02d %02d:%02d:%02d\n",