1.
要求创建一个
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 <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
FILE* fp = fopen("./time.txt","a+");
if(NULL == fp)
{
perror("fopen");
return -1;
}
time_t t;
int num = 1,a;
while(1)
{
while((a=fgetc(fp)) != -1)
{
if(a == 10)
num++;
}
size_t t=time(NULL);
struct tm* info=localtime(&t);
fprintf(fp,"[%d] %d-%02d-%02d %02d;%02d;%02d\n",num,\
info->tm_year+1900, info->tm_mon+1, info->tm_mday,\
info->tm_hour, info->tm_min, info->tm_sec);
fflush(fp);
num++;
sleep(1);
}
fclose(fp);
return 0;
}
2.
用
read
和
write
拷贝一张图片
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
int fo = open(argv[1],O_RDONLY);
char *p = argv[1];
while(*p++!='.');
char cpfile[20]="cpy.";
int i=4;
while(*p){
cpfile[i]=*p;
i++;
p++;
}
int fc = open(cpfile,O_WRONLY|O_CREAT|O_TRUNC,0664);
char buf[20];
while(i=read(fo,buf,20)){
write(fc,buf,i);
}
close(fo);
close(fc);
return 0;
}