time的循环
#include<stdio.h>
#include<time.h>
#include<unistd.h>
#include<stdlib.h>
int main(int argc, const char *argv[])
{
time_t t;
struct tm *info =NULL;
FILE* fd = fopen("./time.txt","a+");
if(fd == NULL)
{
perror("fopen");
return -1;
}
printf("open success\n");
int line=0;
while(1)
{
t=time(NULL);
info=localtime(&t);
while(fgetc((fd))!=EOF)
{
if(getc(fd)==10)
{
line++;
}
}
fprintf(fd,"[%d] %d-%02d-%02d %02d:%02d:%02d\n"\
,line++,info->tm_year+1900,info->tm_mon+1\
,info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);
fflush(fd);
sleep(1);
}
fclose(fd);
return 0;
}
拷贝图片
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
int main(int argc, const char *argv[])
{
umask(0);
int fd=open(argv[1],O_RDONLY);
if(fd<0)
{
perror("open");
return -1;
}
printf("open success\n");
int fp=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0777);
if(fp<0)
{
perror("open");
return -1;
}
printf("open success\n");
char buf[2];
ssize_t res =0;
while(1)
{
bzero(buf,sizeof(buf));
res =read(fd,buf,sizeof(buf)-1);
if(0==res)
break;
write(fp,buf,sizeof(buf)-1);
}
if(close(fd)<0)
{
perror("close");
return -1;
}
printf("close success\n");
return 0;
}