IO第二天作业

1.用read write函数实现文件拷贝

程序

 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
#include <stdlib.h>
#include <string.h>

 int main(int argc, const char *argv[])
 {
     int ph = open("cp.txt",O_WRONLY|O_CREAT|O_TRUNC, 0777);

     if(ph < 0)
     {
         perror("open");
         return -1;
     }
     int ch = open("./01.txt", O_RDONLY);
     char a[10] = "";
     ssize_t res = 0;
     int b = 0;
     while(1)
     {
         res = read(ch, a,sizeof(a)-1);
             if(res == 0)
         {
             break;
         }
         b = write(ph, a, res);
         printf("%s", a);
         bzero(a, sizeof(a));
     }
     close(ph);
     close(ch);

 }

2.更新任务:要求将当前路径下,所有文件的权限及最后- -次的访问时间提取出来,写入到file.txt中! !

程序

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
int main(int argc, const char *argv[])
{
//  int p = open("./file.txt", O_RDWR|O_CREAT|O_TRUNC);//打开file.txt
    FILE * p = fopen("./file.txt", "w");
    DIR * dp = opendir("./");//打开当前目录
    struct stat b;
    ssize_t tav = 0;
    long arr = 0;
    struct dirent * rp = NULL;                                        
    struct tm * info = NULL;
    while(1)
    {
    rp = readdir(dp);
    if(rp == NULL)
    {
        break;
    }
    printf("%s\n", rp->d_name);
    char a[11] = "";
    strcpy(a, rp->d_name);
    stat(a, &b);
    //文件权限
    printf("mode: 0%o\n", b.st_mode&0777);

    //写入权限
    arr = b.st_mode&0777;
//  tav = write(p, &arr,sizeof(a));
// printf("tav %ld \n", tav);
    fprintf(p, "0%lo\n", arr);
    //时间
    long d = b.st_ctime;
    info = localtime(&d);
    printf("%d-%02d-%02d %02d:%02d:%02d\n",\
            info->tm_year+1900,info->tm_mon+1,\
            info->tm_mday,info->tm_hour,\
            info->tm_min, info->tm_sec);
    fprintf(p,"%d-%02d-%02d %02d:%02d:%02d\n",\
            info->tm_year+1900,info->tm_mon+1,\
            info->tm_mday,info->tm_hour,\
            info->tm_min, info->tm_sec);

//  fprintf(p, "%ld\n",d);
//  tav=write(p, &d, sizeof(d)-1);
//   printf("tav %ld\n", tav);
    printf("\n");
    }
        return 0;
}

现象

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值