APUE之open,read,write,close函数应用,制作基本的makefile。
来源:互联网 发布:网页制作美工 编辑:程序博客网 时间:2024/05/19 16:20
条件:将文件A中的内容写入文件B中。其中需要判断文件A,B是否存在。若B存在则删除以前内容,再将A中内容写入。
dd.c
/********************************************************************************* * Copyright: (C) 2014 songyong<handy_skyoutlook.com> * All rights reserved. * * Filename: dd.c * Description: This file * * Version: 1.0.0(2014年12月19日) * Author: sky <handy_sky@outlook.com> * ChangeLog: 1, Release initial version on "2014年12月19日 20时38分08秒" * ********************************************************************************/#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <errno.h>#include <string.h>#define BUF_SIZE 128/******************************************************************************** * Description: * Input Args: * Output Args: * Return Value: ********************************************************************************/int main (int argc, char **argv){ char *src_file; char *dst_file; int fd_src; int fd_dst; char buf[BUF_SIZE]; int len; if(argc != 3) { printf("usage: %s [src_file] [dst_file]\n", argv[0]);//不符合参数个数即报错. return -1; } src_file = argv[1]; dst_file = argv[2]; fd_src = open(src_file,O_RDONLY); if(fd_src < 0) { printf("Open file '%s' failure: %s\n",src_file,strerror(errno)); return 0; } fd_dst = open(dst_file, O_RDWR|O_CREAT|O_TRUNC,0755); if(fd_dst < 0) { printf("Open file '%s' failure: %s\n",dst_file,strerror(errno)); return 0; } while ((len = read(fd_src,buf,sizeof(buf))) > 0) { write(fd_dst,buf,len); } close(fd_src); close(fd_dst); return 0;} /* ----- End of main() ----- */
bins = ddobjs = dd.osrcs = dd.c$(bins) : $(objs) gcc -o dd dd.o$(objs) : $(srcs) gcc -c dd.cclean: rm -f $(bins) (objs)
附上makefile的制作规则:
target ... : prerequisites ...
command
...
...
target也就是一个目标文件,可以是Object File,也可以是执行文件。还可以是一个标签(Label)。
prerequisites就是,要生成那个target所需要的文件或是目标。
command也就是make需要执行的命令。(任意的Shell命令)
这是一个文件的依赖关系,也就是说,target这一个或多个的目标文件依赖于prerequisites中的文件,其生成规则定义在command中。说白一点就是说,prerequisites中如果有一个以上的文件比target文件要新的话,command所定义的命令就会被执行。
我初步理解的决定关系为:
.h or .c文件 → (- c)→.o → (- o)→可执行的目标文件.
gcc:
-o output_filename,确定输出文件的名称为output_filename,同时这个名称不能和源文件同名。如果不给出这个选项,gcc就给出预设的可执行文件a.out。
-c 只编译代码生成object目标文件但不进行链接。
0 0
- APUE之open,read,write,close函数应用,制作基本的makefile。
- 高编基本函数open/close,read/write的应用
- open、read、write、close函数应用实例
- 应用层open(read、write、close)如何调用驱动open(read、write、close)函数的?
- linux下的open()read()write()close()lseek()函数
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- linux下 open() write() read() close函数
- open、close、read、write四个函数
- open close write 跟read
- linux 基本文件操作-----creat open write read close
- 基本IO函数的使用(open,write,read)
- AngularJs多重视图和路由的使用
- Linux平台开发之Samba和Source Insight
- 故障转移和负载均衡之任务总结
- 软件工程第三次作业 团队项目 房屋订租系统需求分析
- HEVC-xCompressCU
- APUE之open,read,write,close函数应用,制作基本的makefile。
- Simplex单纯性算法的Python实现
- unity学习之子弹发射
- Unity打飞机基本脚本
- unity3D__常用核心对象例题
- COCOS2DX 黑屏
- Android--关于Android软键盘
- 解构Unity的腳本物件模型
- 对象内存布局 (1)