linux实习第二天

本日内容讲解了有关linux系统中的缓存的作用,在linux系统中使用man 2 fopen/fclose/fread/fwrite查询手册可以获得使用格式以及返回值。
将第一日所编写的实现copy功能的程序进行深一步的改写,将其中的read、open、write改为系统缓存命令fread、fopen、fwrite,从而利用linux系统中的buf缓存来实现copy文件内容的功能。

#include <stdio.h>
  2 #include <stdlib.h>
  3 #include <sys/types.h>
  4 #include <sys/stat.h>
  5 #include <fcntl.h>
  6 #include <string.h>
  7 
  8 int main(int argc, char *argv[])
  9 {
 10         int  ret;
 11         FILE* fd_from;
 12         FILE* fd_to;
 13 
 14         if (argc != 3)
 15         {
 16                 printf("error\n");
 17                 exit(1);
 18         }
 19         //打开源文件
 20         fd_from = fopen(argv[1], "r+");
 21         if (NULL== fd_from)
 22         {
 23                 perror("open");
    exit(1);
 25         }
 26 
 27         //打开目的文件
 28         fd_to = fopen(argv[2], "w+");
 29         if (NULL== fd_to)
 30         {
 31                 perror("open");
 32                 exit(1);
 33         }
 34 
 35         char buf[32] = {0};
 36 
 37         while ((ret = fread(buf,1,31,fd_from)) != 0)
 38         {
 39                 ret = fwrite(buf,1,ret, fd_to);
 40                 if (0 == ret)
 41                 {
 42                         perror("write");
 43                         break;     //结束循环
5 					}
 46                 memset(buf, 0, 32);  //清空buf
 47         }
 48 
 49         fclose(fd_to);
 50         fclose(fd_from);
 51         return 0;
 52 }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值