六月11日作业

  #include <stdio.h>
  #include <syspes.h>
  #include <sysat.h>
  #include <fcntl.h>
  #include <unistd.h>
  #include <sysat.h>
 
 
  int main(int argc, const char *argv[])
  {
      int fp = open("./1.png", O_RDONLY);
      if(fp < 0)
      {
          perror("open1");
          return -1;
      }
  
      int fo = open("./2.png", O_WRONLY | O_CREAT | O_TRUNC);
      if(fo < 0)
      {
          perror("open2");
          return -1;
      }
  
  
      struct stat buf;
      int s = stat("./1.png", &buf);
      if(s < 0)
      {
          perror("stat");
          return -1;
      }
  
  //  printf("size = %ld\n",buf.st_size);
  
      int size  = buf.st_size / 2;
      printf("k = %d\n",k);
  
      char str = 0;
      int len = 0;
      pid_t pid = fork();
  
      if(pid > 0)
      {
          sleep(2);
          lseek(fp, 0, SEEK_SET);
          lseek(fo, 0, SEEK_SET);
          while( len < szie)
          {
              read(fp, &str, 1);
              write(fo, &str, 1);
              len++;
          }
          printf("前面拷贝完成\n");
      }
      else if(pid == 0)
      {
          len = size;
          lseek(fp, k , SEEK_SET);
          lseek(fo, k , SEEK_SET);
          while(len < buf.st_size)
          {
              read(fp, &str, 1);
              write(fo, &str, 1);
              len++;
          }
          printf("后面拷贝完成\n");
      }
      else
      {
          perror("fork");
          return -1;
      }
  
      close(fp);
      close(fo);                                                                                                 
      return 0;
  }
 
 
 #include <stdio.h>
 #include <sys/types.h>
 #include <dirent.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
 int main(int argc, const char *argv[])
 {
     DIR* dr = opendir("../611_dir/");
     if(NULL == dr)
     {
         perror("opendir");
         return 0;
     }
     FILE* fp = fopen("./4.text", "w");
     if(NULL == fp)
     {
         perror("fopen");
         return 0;
     }
     struct dirent * rdir = NULL;
     struct stat buf;
     while(1)
     {
         rdir = readdir(dr);
         if(NULL == rdir)
             break;
         if(rdir->d_name[0] != '.')
             continue;                                   
         stat("./1.c", &buf);
         fprintf(fp, "%d", buf.st_mode);
         fprintf(fp, "%ld", buf.st_atime);
         printf("\n");
     }
     fclose(fp);
     closedir(dr);
     return 0;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值