1.使用文件IO对图片进行拷贝。要求子进程拷贝后半部分,父进程拷贝前半部分。要求拷贝到一个文件中。拷贝完毕后与源文件一致。2.要求将指定路径下,所有文件(除了隐藏文件)的权限及最后一次的访问时间提取出

 1.使用文件IO对图片进行拷贝。要求子进程拷贝后半部分,父进程拷贝前半部分。要求拷贝到一个文件中。拷贝完毕后与源文件一致。

  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <sys/stat.h>
  4 #include <fcntl.h>
  5 #include <unistd.h>
  6 int main(int argc, const char *argv[])
  7 {
  8      int path = open("./1.jpg", O_RDONLY);
  9      if (path < 0)
 10      {
 11         perror("open");
 12         return -1;
 13      }
 14      printf("文件打开成功\n");
 15 
 16      int path1= open("./2.jpg",O_WRONLY | O_CREAT | O_TRUNC, 0777);
 17      if(path1 < 0)
 18      {
 19         perror("open");
 20         return -1;
 21      }
 22     printf("文件打开成功\n");
 23 
 24      off_t res = lseek(path,0,SEEK_END);
 25      pid_t pid =fork();
 26 
 27 
 28 
 29     if (pid >0)
 30     {
 31         sleep(2);
 32         lseek(path,0,SEEK_SET);
 33         lseek(path1,0,SEEK_SET);
 34         char picture;
 35         for(int i =0;i<res/2;i++)
 36         {
 37             read(path,&picture,1);
 38             write(path1,&picture,1);
 39         }
 40 
 41         printf("父进程读取完毕\n");                                                                   
 42     }
 43     else if ( pid ==0)
 44     {
 45         lseek(path,res / 2,SEEK_SET);
 46         lseek(path1,res / 2,SEEK_SET);
 47         char picture;
 48         for(int i =res / 2;i < res ; i++)
 49     {
 50         read(path,&picture,1);
 51         write(path1,&picture,1);
 52     }
 53         printf("子进程读取完毕\n");
 54     }
 55     else
 56     {
 57         perror("fork");
 58         return -1;
 59     }
 60     while(1)
 61     {
 62         sleep(1);
 63     }
 64     close(path);
 65     return 0;
 66 }

运行结果

2.要求将指定路径下,所有文件(除了隐藏文件)的权限及最后一次的访问时间提取出

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <dirent.h>
 4 #include <errno.h>
 5 #include <string.h>
 6 #include <sys/stat.h>
 7 #include <time.h>
 8 #include <unistd.h>
 9 
10 int main(int argc, const char *argv[])
11 {
12     DIR * dp = opendir("../目录函数/");
13     if (NULL  == dp)
14     {
15         perror("errno");
16         return -1;
17     }
18     printf("文件夹打开成功\n");
19 
20     FILE * path= fopen("./file.txt","w");
21     if(path < 0)
22     {
23         perror("fopen");
24         return -1;
25     }                                                                                       
26     printf("文件打开成功\n");
27 
28     struct dirent * rp;
29     struct stat charect;
30 
31     int i = 0;
32     while (1)
33     {
34         rp = readdir(dp);
35         if(NULL == rp)
36         {
37             if(0 == errno)
38             {
39                 printf("目录读取完毕\n");
40                 break;
41             }
42             else
43             {
44                 perror("readdir");
45                 return -1;
46             }
47         }
48         if (rp->d_name[0] == '.')
49         {
50             continue;
51         }
52 
53             if (stat("../目录函数/",&charect) <0)
54             {
55                 perror("stat");
56                 return -1;
57             }
58             fprintf(path,"mode=%o time=%ld name=%s\n", charect.st_mode & 0777,\
59                     charect.st_ctime,rp->d_name);
60         }
61 
62     if(closedir(dp)<0)
63     {
64         perror("closedir");
65         return -1;
66     }
67     fclose(path);
68     return 0;
69 }

运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值