思维导图:
练习:
1将当前的时间写入到time. txt的文件中,如果ctrl+c退出之后,在再次执行支持断点续写
1.2022-04-26 19:10:20
2.2022-04-26 19:10:21
3.2022-04-26 19:10:22
//按下ctrl+c停止,再次执行程序
4.2022-04-26 20:00:00
5.2022-04-26 20:00:01
#include <head1.h>
int main(int argc, const char *argv[])
{
FILE* fp=fopen("./time.text","r+");
if(fp==NULL)
{
PRINT_ERROR("error");
}
/* int i=1;
while(1)
{
int ch =fgetc(fp);
if(ch==EOF){break;}
if(ch==10)
{
i++;
}
}
*/
int i=fgetc(fp);
i++;
while(1)
{
time_t rec;
time_t res=time(&rec);
struct tm *t=localtime(&rec);
if(NULL==t)
{
PRINT_ERROR("error");
}
printf("%d.%d-%d-%d %02d:%02d:%02d\n",i,t->tm_year+1900,t->tm_mon+1\
,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
fputc(i++,fp);
rewind(fp);
sleep(1);
}
return 0;
}
运行结果:
2.使用fwrite和fread函数实现图片的拷贝
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <head1.h>
int main(int argc, const char *argv[])
{
FILE* fp=fopen("2025-03-04 05-30-32 的屏幕截图.png","r")
FILE* fp1=fopen("picture.text","w");
if(fp==NULL)
{
PRINT_ERROR("error");
}
if(fp1==NULL)
{
PRINT_ERROR("error");
}
char str[128]={};
while((fread(str,sizeof(char),1,fp))!=0)
{
if((fwrite(str,sizeof(char),1,fp1))==0)
{
return -1;
}
}
fclose(fp);
fclose(fp1);
return 0;
}