IO7.28

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值