copy.c:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <string.h>
  7.  
  8. //a.out src.txt dest.txt
  9. //argv[0] argv[1] argv[2]
  10. int main(int argc,char *argv[])
  11. {
  12. char src[] = {};
  13. char dest[] = {};
  14. int fdr;
  15. int fdw;
  16. int ret;
  17. char buf[] = {};
  18.  
  19. if(argc != ){
  20. printf("Usage: %s src.txt dest.txt\n",argv[]);
  21. return -;
  22. }
  23. strcpy(src,argv[]);
  24. strcpy(dest,argv[]);
  25.  
  26. /*1.以的方式打开源文件src.txt*/
  27. fdr = open(src,O_RDONLY);
  28. if(fdr < ){
  29. perror("open");
  30. return -;
  31. }
  32.  
  33. /*2.以写的方式打开目标文件 dest.txt ,如果不存在,则创建*/
  34. fdw = open(dest,O_WRONLY | O_CREAT,);
  35. if(fdw < ){
  36. perror("open");
  37. return -;
  38. }
  39.  
  40. /*3.把src文件的指针移动到文件头*/
  41. lseek(fdr,,SEEK_SET);
  42.  
  43. /*3.循环的读取src.txt ,直到结束*/
  44. while()
  45. {
  46. memset(buf,,sizeof(buf)); //清零
  47. ret = read(fdr,buf,sizeof(buf)-);
  48. if(ret > ){
  49.  
  50. /*4.把读到的内容写入到 dest.txt*/
  51. write(fdw,buf,ret);
  52. }else if( == ret){
  53.  
  54. /*读取结束*/
  55. printf("read over!\n");
  56. break;
  57. }else{
  58. perror("read");
  59. break;
  60. }
  61. }
  62.  
  63. /*5.关闭*/
  64. close(fdr);
  65. close(fdw);
  66.  
  67. return ;
  68. }

文件I/O实现cp复制功能的更多相关文章

  1. linux使用su切换用户提示 Authentication failure的解决方法& 复制文件时,报cp: omitting directory `XXX'

    linux使用su切换用户提示 Authentication failure的解决方法:这个问题产生的原因是由于ubtun系统默认是没有激活root用户的,需要我们手工进行操作,在命令行界面下,或者在 ...

  2. linux 复制文件时,报cp: omitting directory `XXX'

    今天在用linux命令进行文件复制时omitting cp -i BBS /opt/workspace/apache-tomcat-6,参数用的是 -i),所以也不太熟悉,原来,还有子目录文件,而是必 ...

  3. Linux文件与目录管理 cp od chattr lsattr

    1:在shell脚本中,一定要使用绝对路径. 2:在根目录下,.和..是完全相同的两个目录. 3:cd - 就相当于撤销,表示回到前面状态所在的目录. 4:mkdir -m 700 test 加&qu ...

  4. Linux cp一个文件夹时提示cp: omitting directory `test/'

    将一个文件夹test 复制到地址/opt/tmp下,提示出错: cp: omitting directory `test/' 原因: test 目录下还有目录,不能直接进行拷贝. 我们先找下cp 的命 ...

  5. Linux find查找指定文件 按照名称 然后cp拷贝到指定目录且指定文件名

    最近有一个需求,需要将指定目录下的文件(已知文件名)复制到另一个指定的目录且重命名文件. 要求: 在var目录下会定义系统的启动日志相关信息,请查找对应的boot.log文件,并把它备份一份到var/ ...

  6. 复制文件或目录命令 - cp

    1) 命令名称:cp 2) 英文原意:copy 3) 命令所在路径:/bin/cp 4) 执行权限:所有用户 5) 功能描述:复制文件或目录 语法: cp -rp [原文件或目录][目标目录] -r ...

  7. 关于so文件cp覆盖导致调用者core的研究

    先说cp好mv/rm的区别: cp from to,则被覆盖文件 to的inode依旧不变(属性也不变),内容变为from的: mv from to,则to的inode变为from的,相应的,to的属 ...

  8. linux-14基础命令之-复制(cp)移动(mv),删除(rm),拷贝文件(dd)

    1.cp 命令用于复制文件或者目录 格式为:cp[选项]源文件  目标文件 复制名录有三种情况: @1.目标文件是一个目录,将源复制到该目录下:  @2.目标文件是一个文件,将源文件覆盖该文件: @3 ...

  9. linux常用命令系列—cp 复制文件与文件夹

    原文地址:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=2272&id=37363 指令名称:cp(copy)功能介绍 ...

随机推荐

  1. Android面试题集锦 (转)

    转自:http://xiechengfa.iteye.com/blog/1044721 一些常见的Android面试基础题做下总结,看看你能做出多少道? 1. Intent的几种有关Activity启 ...

  2. 第三十一节,time时间模块

    模块简介 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...

  3. 《JS权威指南学习总结--3.4null和undefined》

    内容要点 一.相似性  var a= undefined;       var b= null;       if(a==b){           alert("相等");   ...

  4. LeetCode OJ 337. House Robber III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  5. Android PopupWindow菜单

    初学Android,引用了这篇文章的代码 http://www.cnblogs.com/jiezzy/archive/2012/08/15/2640584.html 使用PopupWindow制作自定 ...

  6. asp中的动态数组

    <% Dim array1(),i ReDim array1(3)array1(3)=10response.Write(array1(3)&"<br>") ...

  7. Server的Transfer和Response的Redirect

    在实现页面跳转的时候,有些人喜欢用Response.Redirect,而有些人则喜欢用Server.Transfer.大部分时间似乎这两种方法都可以实现相同的功能,那究竟有区别吗? 查了些文档,发现两 ...

  8. 转 excel表怎么自动分列

    http://jingyan.baidu.com/article/656db918fc3501e380249c53.html

  9. Anton and School

    Anton and School time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  10. Arch: Configurations

    the original purpose is to show the steps needed to setup i3 in vbox.. easy. alright, it is a bit mi ...