IO day 3

作业:
1. 用read函数完成文件大小计算
2. 将课上的的文件权限提取修改成循环方式

3.xmind

1.

  1 #include <stdio.h>                                                                        
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <myku.h>
  5 #include <sys/stat.h>
  6 #include <sys/types.h>
  7 #include <fcntl.h>
  8 int main(int argc, const char *argv[])
  9 {
 10     int a=open("./2.c",O_RDONLY);
 11     if(a<0)
 12     {
 13         ERR_MSG("open");
 14         return -1;
 15     }
 16 //  off_t s=lseek(a,0,SEEK_END);
 17 //  printf("%ld\n",s);
 18 
 19     char c;
 20     int count=0;
 21     while(read(a,&c,sizeof(c))!=0)
 22     {
 23         count++;
 24     }
 25     printf("%d\n",count);
 26     close(a);
 27     return 0;
 28 }

2.

  1 #include <stdio.h>                                                                        
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <myku.h>
  5 #include <sys/stat.h>
  6 #include <sys/types.h>
  7 #include <fcntl.h>
  8 #include <time.h>
  9 void modes(mode_t n)
 10 {
 11     int s=00400;
 12     while(s>=01)
 13     {
 14         if(n&s)
 15             putchar('r');
 16         else
 17             putchar('-');
 18         s=s/2;
 19         if(n&s)
 20             putchar('w');
 21         else
 22             putchar('-');
 23         s=s/2;
 24         if(n&s)
 25             putchar('x');
 26         else
 27             putchar('-');
 28         s=s/2;
 29     }
 30     putchar(10);
 31     return;
 32 }
 33 int main(int argc, const char *argv[])
 34 {   
 35     struct stat a;
 36     if(stat("2.c",&a)<0)
 37     {   
 38         ERR_MSG("stat");
 39         return -1;
 40     }
 41     
 42     printf("mode=0%o\n",a.st_mode);
 43     modes(a.st_mode);
 44     printf("ctime:%ld\n",a.st_ctime);
 45     printf("atime:%ld\n",a.st_atime);
 46     printf("mtime:%ld\n",a.st_mtime);
 47     printf("time:%ld\n",time(NULL));
 48     return 0;
 49 }

 

3.xmind

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值