创建一个文件test.txt,每隔1秒向文件里写入一行数据
001,2023-4-3 17:29:50
002,2023-4-3 17:29:51
003,2023-4-3 17:29:52
该程序应该无限循环,直到Ctrl + C终端程序
再次启动这个程序时可以追加到源文件之后,并且序号能够接上上次的序号
004,2023-4-3 17:31:20
005,2023-4-3 17:31:21
1、睡眠函数sleep(1);
2、使用time函数来获取“当前的时间”
3、localtime函数来转换成年月日时分秒
#include<stdio.h>
#include<time.h>
#include<unistd.h>
#include<string.h>
int main(){
int num=0;
char c;
FILE *fp=fopen("file1.txt","a+");
if(fp==NULL){
perror("fopen error");
return -1;
}
char buf[5];
fgets(buf,6,fp);
if(strncmp(buf,"01",2)!=0){
remove("file.txt");
fp=fopen("file1.txt","w+");
}
fseek(fp,