1.格式化输出函数
2.输出日期到文本文档和数组中
#include<stdio.h>
int main()
{
int year=2021,month=4,date=8;
FILE *fd;
char buf[64];
fd=fopen("data.txt","a+");
fprintf(fd,"%d-%d-%d\n",year,month,date);
sprintf(buf,"%d-%d-%d\n",year,month,date);
int count=0;
while(buf[count]!='\0'){
count++;
}
printf("buf=%scount=%d\n",buf,count);
return 0;
}
3.练习
3.1 题目
3.2 提示
3.3 实现
#include<stdio.h>
#include<unistd.h>
#include<time.h>
#include<string.h>
int main()
{
FILE *fp;
char buf[64];
int line=0;
time_t t;
struct tm* tp;
fp=fopen("./test.txt","a+");
if(fp==NULL){
perror("fopen error");
return -1;
}
while(fgets(buf,64,fp)!=NULL){
if(buf[strlen(buf)-1]=='\n'){
line++;
}
}
while(1){
time(&t);
tp=localtime(&t);
fprintf(fp,"%02d,%d-%02d-%02d %02d:%02d:%02d\n",
++line,tp->tm_year+1900,tp->tm_mon+1,tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);
fflush(fp);
sleep(3);
}
fclose(fp);
return 0;
}