3/28作业

该文章展示了一个C语言程序,用于打印指定目录下所有文件的详细信息,包括文件类型、权限、所有者、所属组、大小和修改时间。同时,程序还演示了如何创建子进程,主进程打印图片的前半部分,子进程打印后半部分,实现了图片内容的分割处理。
摘要由CSDN通过智能技术生成

1、利用dir函数和stat函数实现打印目录下所有文件的信息

程序代码

  1 #include<stdio.h>
  2 #include<sys/types.h>
  3 #include<dirent.h>
  4 #include<errno.h>
  5 #include<sys/stat.h>
  6 #include<unistd.h>
  7 #include<time.h>
  8 #include<pwd.h>
  9 #include<grp.h>
 10 
 11 void Mode(mode_t mode);
 12 void Time(time_t Time_t);
 13 void Mode_type(mode_t mode);
 14 void  Stat(char *pathname);
 15 
 16 int main(int argc, const char *argv[])
 17 {
 18     char str[256]="";
 19     scanf("%s",str);
 20     DIR* dp = opendir(str);
 21     if(NULL == dp)
 22     {
 23         perror("opendir");
 24         return -1;
 25     }
 26 
 27     struct dirent *read = NULL;
 28 
 29     while(1)
 30     {
 31         read = readdir(dp);
 32         if(read == NULL)
 33         {
 34             if(errno == 0)
 35             {
 36                 break;
 37             }
 38             else
 39             {
 40                 perror("readdir");
 41                 return -1;
 42             }
 43         }
 44         else
 45         {
 46             Stat((read->d_name));
 47         }
 48     }
 49     if(closedir(dp) < 0)
 50     {
 51         perror("closedif");
 52         return -1;                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 53     }
 54     return 0;
 55 }
 56 void  Stat(char *pathname)
 57 {
 58     struct stat buf;
 59 
 60     if(stat(pathname,&buf) < 0)
 61     {
 62         perror("stat");
 63         return ;
 64     }
 65 
 66     //提取文件类型
 67     Mode_type(buf.st_mode);
 68 
 69     //调用权限函数
 70     Mode(buf.st_mode);
 71 
 72     //提取软连接数
 73     printf("%ld ",buf.st_nlink);
 74 
 75     //提取所属用户
 76     struct passwd *name = getpwuid(buf.st_uid);
 77     printf("%s ",(name->pw_name));
 78 
 79     //提取文件所属组用户
 80     struct group *grp_name = getgrgid(buf.st_gid);
 81     if(NULL == grp_name)
 82     {
 83         perror("getgrgid");
 84         return ;
 85     }
 86     printf("%s ",grp_name->gr_name);
 87 
 88     //提取文件大小
 89     printf("%ld ",buf.st_size);
 90 
 91     //提取修改时间
 92     Time(buf.st_mtim.tv_sec);
 93 
 94     //打印文件名
 95     printf("%s\n",pathname);
 96     return ;
 97 
 98 }
 99 
100 //封装一个权限函数
101 void Mode(mode_t mode)
102 {
103     char arr[3]="rwx";
104     char buf[10]="";
105     int n=0400;
106 
107     for(int i = 0;i<9;i++)
108     {
109         if((mode & (n>>i)) == 0)
110             buf[i]='-';
111         else
112             buf[i]=arr[i%3];
113         printf("%c",buf[i]);
114     }
115     putchar(' ');
116 }
117 
118 void Mode_type(mode_t mode)
119 {
120     char str;
121     if(S_ISBLK(mode)) str = 'b';
122     else if(S_ISCHR(mode)) str = 'c';
123     else if(S_ISDIR(mode)) str = 'd';
124     else if(S_ISREG(mode)) str = '-';
125     else if(S_ISLNK(mode)) str = 'l';
126     else if(S_ISSOCK(mode)) str = 's';
127     else if(S_ISFIFO(mode)) str = 'p';
128     printf("%c",str);
129 }
130 
131 
132 void Time(time_t Time_t)
133 {
134     struct tm *tt=NULL;
135     if((tt=localtime(&Time_t)) < 0)
136     {
137         perror("localtime");
138         return;
139     }
140 
141     printf("%d月 %d %d:%d ",tt->tm_mon,tt->tm_mday,tt->tm_hour,tt->tm_min);
142 }
143 
~                                

运行结果

ubuntu@ubuntu:文件IO$ gcc 07_dir.c
ubuntu@ubuntu:文件IO$ ./a.out 
./
drwxr-xr-x 42 ubuntu ubuntu 4096 2月 28 17:5 ..
drwxrwxr-x 2 ubuntu ubuntu 4096 2月 28 17:5 进程
-rwxrwxr-x 1 ubuntu ubuntu 13184 2月 28 17:10 a.out
-rw-rw-r-- 1 ubuntu ubuntu 0 2月 28 11:42 07_dir.h
-rw-rw-r-- 1 ubuntu ubuntu 1531 2月 28 11:53 stat.h
-rw-rw-r-- 1 ubuntu ubuntu 545 2月 28 11:41 06_dit.c
-rw-rw-r-- 1 ubuntu ubuntu 35 2月 28 11:40 huahua.c
-rw-rw-r-- 1 ubuntu ubuntu 2483 2月 28 17:6 07_dir.c
-rw-rw-r-- 1 ubuntu ubuntu 1580 2月 28 11:16 stat.c
drwxrwxr-x 3 ubuntu ubuntu 4096 2月 28 17:10 .
-rw-rw-r-- 1 ubuntu ubuntu 1720752 2月 28 11:23 stat.h.gch

2、打开一张图片,主进程打印前一半,子进程打印后一半

程序代码:

  1 #include<stdio.h>
  2 #include<sys/types.h>
  3 #include<unistd.h>
  4 #include<sys/stat.h>
  5 #include<fcntl.h>
  6 
  7 int main(int argc, const char *argv[])
  8 {
  9     //打开目标图片以及创建一个文件保存图片内容
 10     int fd1 = open("./2023-02-14 10-49-42 的屏幕截图.png",O_RDONLY);
 11     int fd2 = open("./1.png",O_WRONLY|O_TRUNC|O_CREAT,0664);
 12 
 13     //找到图片文件的中间位置
 14     int num=lseek(fd1,0,SEEK_END)/2;
 15     if(num < 0)
 16     {
 17         perror("lseek");
 18         return -1;
 19     }
 20 
 21     //修改文件偏移量到文件开头
 22     lseek(fd1,0,SEEK_SET);
 23 
 24     char a;
 25 
 26     pid_t sid=fork();
 27     if(sid < 0)
 28     {
 29         perror("fork");
 30         return -1;
 31     }
 32 
 33     //子进程打印后半部分
 34     else if(0 == sid)
 35     {
 36         //判断文件偏移量是否位于中间位置
 37         while(lseek(fd1,0,SEEK_CUR) != num);
 38 
 39         //打印后半部分
 40         while(1)
 41         {
 42             if(0 == read(fd1,&a,1))
 43             {
 44                 printf("子进程打印完成\n");
 45                 break;
 46             }
 47             write(fd2,&a,1);
 48 
 49         }
 50 
 51     }
 52 
 53     //父进程打印前半部分
 54     else if(0 < sid)
 55     {
 56         while(1)
 57         {
 58             read(fd1,&a,1);
 59             write(fd2,&a,1);
 60             if(num == lseek(fd1,0,SEEK_CUR))
 61             {                                                                                                                                                                                                                     
 62                 printf("父进程前半部分打印完成\n");
 63                 break;
 64             }
 65         }
 66     }
 67 
 67 
 68     while(1)
 69         sleep(1);
 70 
 71     return 0;                                                                                       
 72 }
~                      

运行结果

ubuntu@ubuntu:进程$ gcc copy_pic1.c 
ubuntu@ubuntu:进程$ ./a.out 
父进程前半部分打印完成
子进程打印完成
^C
ubuntu@ubuntu:进程$ ls
 1.png   1.txt  '2023-02-14 10-49-42 的屏幕截图.png'   a.out   copy_pic1.c   copy_pic2.c   jincheng.c
ubuntu@ubuntu:进程$ ls -l
总用量 908
-rw-rw-r-- 1 ubuntu ubuntu 449749 三月 28 18:54  1.png
-rw-rw-r-- 1 ubuntu ubuntu      2 三月 28 18:32  1.txt
-rw-rw-r-- 1 ubuntu ubuntu 449749 三月 28 18:37 '2023-02-14 10-49-42 的屏幕截图.png'
-rwxrwxr-x 1 ubuntu ubuntu   8656 三月 28 18:54  a.out
-rw-rw-r-- 1 ubuntu ubuntu   1165 三月 28 18:54  copy_pic1.c
-rw-rw-r-- 1 ubuntu ubuntu    576 三月 28 18:30  copy_pic2.c
-rw-rw-r-- 1 ubuntu ubuntu    576 三月 28 17:29  jincheng.c
ubuntu@ubuntu:进程$ diff 1.png 2023-02-14\ 10-49-42\ 的屏幕截图.png 
ubuntu@ubuntu:进程$ 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

malingshu404

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值