1.用read和write拷贝一张图片
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int a1= open("./pic.jpg",O_RDONLY);
int a2= open("./cpy.jpg",O_WRONLY|O_CREAT|O_TRUNC,0777);
char* arr[10];
ssize_t c=999;
while((c = read (a1,arr,9))>0)
{
// printf("c=%d",c);
// printf("%d",strlen(arr));
write(a2,arr,c);
}
close(a1);
close(a2);
return 0;
}
2.要求创建一个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>
int line_num(FILE*p)
{
int count=0;
int p1;
while((p1=fgetc(p))!=EOF)
{
if(p1=='\n')
{
count++;
}
}
return count;
}
int main(int argc, const char *argv[])
{
FILE *fp=fopen("./time.txt","a+");
if (fp==NULL)
{
perror("fopen");
return -1;
}
int line=line_num(fp);
time_t t;
fseek(fp,0,SEEK_END);
for(int i=line+1;i>-1;i++)
{
t=time(NULL);
struct tm *cur_t=localtime(&t);
fprintf(fp,"[%d] %d-%02d-%02d %02d-%02d-%02d\n"\
,i,cur_t->tm_year+1900,cur_t->tm_mon+1\
,cur_t->tm_mday,cur_t->tm_hour,cur_t->\
tm_min,cur_t->tm_sec);
fflush(fp);
sleep(1);
}
fclose(fp);
return 0;
}