要求创建一个time.txt,存储内容格式如下:
[1] 2022-07-28 17:15:06
[2] 2022-07-28 17:15:07
[3] 2022-07-28 17:15:08
ctrl + c退出程序,过一会儿之后重新启动程序
[1] 2022-07-28 17:15:06
[2] 2022-07-28 17:15:07
[3] 2022-07-28 17:15:08 <-------------------
[4] 2022-07-28 17:16:31
[5] 2022-07-28 17:16:32
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(int argc, const char *argv[])
{
FILE *fb=fopen("./time.txt","a+");
if(NULL==fb)
{
perror("fopen");
return -1;
}
time_t t;
struct tm*info=NULL;
char a;
while(1)
{
t=time(NULL);
info=localtime(&t);
fseek(fb,0,SEEK_SET);
int i=0;
while((a=fgetc(fb))!=-1)
{
if(a=='\n')
i++;
}
printf("[%d]",i+1);
printf("%d-%02d-%02d %02d:%02d:%02d\n",info->tm_year+1900,info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);
fprintf(fb,"[%d] %d-%02d-%02d %02d:%02d:%02d\n",i+1,info->tm_year+1900,info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);
fflush(stdout);
sleep(1);
}
return 0;
}
- 要求文件IO拷贝一张图片; eog 4.png
- 附加:用标准IO拷贝一张图片
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define MSG_ERR(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
int fb1=open("./2.png",O_RDONLY);
if(fb1<0)
{
MSG_ERR("open");
return -1;
}
int fb2=open("./copy.png",O_RDWR|O_CREAT|O_TRUNC,0777);
if(fb2<0)
{
MSG_ERR("open");
return -1;
}
char c;
while(1)
{
int res=read(fb1,&c,1);
if(0==res)
break;
write(fb2,&c,1);
}
close(fb1);
close(fb2);
return 0;
}
3972

被折叠的 条评论
为什么被折叠?



