作业:
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