文件I/O实现cp复制功能
copy.c:
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- //a.out src.txt dest.txt
- //argv[0] argv[1] argv[2]
- int main(int argc,char *argv[])
- {
- char src[] = {};
- char dest[] = {};
- int fdr;
- int fdw;
- int ret;
- char buf[] = {};
- if(argc != ){
- printf("Usage: %s src.txt dest.txt\n",argv[]);
- return -;
- }
- strcpy(src,argv[]);
- strcpy(dest,argv[]);
- /*1.以的方式打开源文件src.txt*/
- fdr = open(src,O_RDONLY);
- if(fdr < ){
- perror("open");
- return -;
- }
- /*2.以写的方式打开目标文件 dest.txt ,如果不存在,则创建*/
- fdw = open(dest,O_WRONLY | O_CREAT,);
- if(fdw < ){
- perror("open");
- return -;
- }
- /*3.把src文件的指针移动到文件头*/
- lseek(fdr,,SEEK_SET);
- /*3.循环的读取src.txt ,直到结束*/
- while()
- {
- memset(buf,,sizeof(buf)); //清零
- ret = read(fdr,buf,sizeof(buf)-);
- if(ret > ){
- /*4.把读到的内容写入到 dest.txt*/
- write(fdw,buf,ret);
- }else if( == ret){
- /*读取结束*/
- printf("read over!\n");
- break;
- }else{
- perror("read");
- break;
- }
- }
- /*5.关闭*/
- close(fdr);
- close(fdw);
- return ;
- }
文件I/O实现cp复制功能的更多相关文章
- linux使用su切换用户提示 Authentication failure的解决方法& 复制文件时,报cp: omitting directory `XXX'
linux使用su切换用户提示 Authentication failure的解决方法:这个问题产生的原因是由于ubtun系统默认是没有激活root用户的,需要我们手工进行操作,在命令行界面下,或者在 ...
- linux 复制文件时,报cp: omitting directory `XXX'
今天在用linux命令进行文件复制时omitting cp -i BBS /opt/workspace/apache-tomcat-6,参数用的是 -i),所以也不太熟悉,原来,还有子目录文件,而是必 ...
- Linux文件与目录管理 cp od chattr lsattr
1:在shell脚本中,一定要使用绝对路径. 2:在根目录下,.和..是完全相同的两个目录. 3:cd - 就相当于撤销,表示回到前面状态所在的目录. 4:mkdir -m 700 test 加&qu ...
- Linux cp一个文件夹时提示cp: omitting directory `test/'
将一个文件夹test 复制到地址/opt/tmp下,提示出错: cp: omitting directory `test/' 原因: test 目录下还有目录,不能直接进行拷贝. 我们先找下cp 的命 ...
- Linux find查找指定文件 按照名称 然后cp拷贝到指定目录且指定文件名
最近有一个需求,需要将指定目录下的文件(已知文件名)复制到另一个指定的目录且重命名文件. 要求: 在var目录下会定义系统的启动日志相关信息,请查找对应的boot.log文件,并把它备份一份到var/ ...
- 复制文件或目录命令 - cp
1) 命令名称:cp 2) 英文原意:copy 3) 命令所在路径:/bin/cp 4) 执行权限:所有用户 5) 功能描述:复制文件或目录 语法: cp -rp [原文件或目录][目标目录] -r ...
- 关于so文件cp覆盖导致调用者core的研究
先说cp好mv/rm的区别: cp from to,则被覆盖文件 to的inode依旧不变(属性也不变),内容变为from的: mv from to,则to的inode变为from的,相应的,to的属 ...
- linux-14基础命令之-复制(cp)移动(mv),删除(rm),拷贝文件(dd)
1.cp 命令用于复制文件或者目录 格式为:cp[选项]源文件 目标文件 复制名录有三种情况: @1.目标文件是一个目录,将源复制到该目录下: @2.目标文件是一个文件,将源文件覆盖该文件: @3 ...
- linux常用命令系列—cp 复制文件与文件夹
原文地址:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=2272&id=37363 指令名称:cp(copy)功能介绍 ...
随机推荐
- Android面试题集锦 (转)
转自:http://xiechengfa.iteye.com/blog/1044721 一些常见的Android面试基础题做下总结,看看你能做出多少道? 1. Intent的几种有关Activity启 ...
- 第三十一节,time时间模块
模块简介 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...
- 《JS权威指南学习总结--3.4null和undefined》
内容要点 一.相似性 var a= undefined; var b= null; if(a==b){ alert("相等"); ...
- 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 ...
- Android PopupWindow菜单
初学Android,引用了这篇文章的代码 http://www.cnblogs.com/jiezzy/archive/2012/08/15/2640584.html 使用PopupWindow制作自定 ...
- asp中的动态数组
<% Dim array1(),i ReDim array1(3)array1(3)=10response.Write(array1(3)&"<br>") ...
- Server的Transfer和Response的Redirect
在实现页面跳转的时候,有些人喜欢用Response.Redirect,而有些人则喜欢用Server.Transfer.大部分时间似乎这两种方法都可以实现相同的功能,那究竟有区别吗? 查了些文档,发现两 ...
- 转 excel表怎么自动分列
http://jingyan.baidu.com/article/656db918fc3501e380249c53.html
- Anton and School
Anton and School time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Arch: Configurations
the original purpose is to show the steps needed to setup i3 in vbox.. easy. alright, it is a bit mi ...