第十八次作业 ——文件IO

 1.要求将指定路径下,所有文件(除了隐藏文件)的权限及最后一次的访问时间提取出来,写入到file.txt中!!提示: opendir readir stat(路径及文件名)-->提取出来的数据写入到file.txt中

2.文件复制

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
    FILE *fd=fopen("1.jpeg","r");
    if(NULL==fd)
    {
        perror("fd open");
        return -1;
    }
    FILE *fd1=fopen("11.jpeg","w+");
    if(NULL==fd1)
    {
        perror("fd1 open");
        return -1;
    }
    pid_t fk=fork();
    if(fk < 0)
    {
        perror("fk fork");
        return -1;
    }
    char buf;
    struct stat sz;
    int st;
    st=stat("1.jpeg",&sz);
    if(st<0)
    {
    perror("st stat");
    return -1;
    }
    int k=1;
    if(0 < fk)
    {
        for(int i=0;i<sz.st_size/2;i++)
        {
        buf=fgetc(fd);
        fputc(buf,fd1);
        }
    }
    else if (0 == fk)
    {
        off_t t;
        t=fseek(fd,(-sz.st_size/2-1),SEEK_END);
        for(int j=0;j<=sz.st_size/2;j++)
        {
            buf=fgetc(fd);
            fputc(buf,fd1);
        }
    }
    else
    {
    perror("fk");
    return -1;
    }
    fclose(fd);
    fclose(fd1);

    return 0;
}                                                                                                       
                                                                                                        

运行结果 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值