APUE习题4.6源代码----实现自己的简易 cp 命令
来源:互联网 发布:感人 知乎 编辑:程序博客网 时间:2024/04/30 20:51
原题:
编写一个类似 cp 的程序,它复制包含空洞的文件,但不将字节0写到输出文件中去。
源代码:
#include <stdio.h>#include <unistd.h>#include <fcntl.h>#include <string.h>#include <stdlib.h>#define BUF_SIZ 128int my_cp( const char *file1, const char *file2 ){ int fd1, fd2; char buffer[BUF_SIZ]; int res; int current_position = 0; int byte_count = 0; fd1 = open( file1, O_RDWR ); if( -1 == fd1 ) { perror("open file1 failed"); return -1; } fd2 = open( file2, O_RDWR | O_APPEND | O_CREAT | O_TRUNC, 0666 ); if( -1 == fd2 ) { perror("open file2 failed"); return -1; } memset( buffer, '\0', BUF_SIZ ); res = read( fd1, buffer, BUF_SIZ ); if( -1 == res ) { perror("file1 read error"); return -1; } while( 0 != res ) { byte_count = 0; for( current_position=0; current_position<BUF_SIZ; current_position++ ) { if( '\0' != buffer[current_position] ) { buffer[byte_count] = buffer[current_position]; byte_count++; } } res = write( fd2, buffer, byte_count ); if( -1 == res ) { perror("file2 write failed"); return -1; } memset( buffer, '\0', BUF_SIZ ); res = read( fd1, buffer, BUF_SIZ ); if( -1 == res ) { perror("file1 read failed"); return -1; } }}int main(int argc, char *argv[]){ if( 3 != argc) { printf("correct usage: ./my_cp file1 file2\n"); exit( EXIT_FAILURE ); } //产生一个含有空洞的文件 file1 int fd = open( argv[1], O_RDWR | O_CREAT | O_TRUNC, 0666 ); if( -1 == fd ) { perror("open failed"); exit( EXIT_FAILURE ); } int res = write( fd, "Hey,man!", strlen("Hey,man!") ); if( -1 == res ) { perror("file1 write failed"); exit( EXIT_FAILURE ); } off_t offset = lseek( fd, 10000, SEEK_SET ); if( -1 == offset ) { perror("lseek failed"); exit( EXIT_FAILURE ); } res = write( fd, "How are you?", strlen("How are you?") ); if( -1 ==res ) { perror("file1 write failed"); exit( EXIT_FAILURE ); } //应用 my_cp 函数拷贝文件 res = my_cp( argv[1], argv[2] ); if( -1 == res ) { perror("copy failed"); exit( EXIT_FAILURE ); } exit( EXIT_SUCCESS );}
运行结果:
[zhang@localhost APUE]$ ./my_cp file1 file2
[zhang@localhost APUE]$ cat file2
Hey,man!How are you?
输出文件在本例中为file2,大小为 20 Bytes = strlen("Hey,man!") + strlen("How are you?")。测试通过。
0 0
- APUE习题4.6源代码----实现自己的简易 cp 命令
- APUE习题3.2----自己实现的dup2( )函数的源码
- APUE习题3.2----自己实现的dup2( )函数的源码
- 《APUE.3E》习题4.6编写自己的cp(l)程序,它复制包含空洞的文件,但不将字节0包含到输出文件中去
- cp命令的实现
- cp命令的实现
- LINUX下的CP命令的源代码
- LINUX下的CP命令的源代码
- apue习题12.2实现
- linux 的cp命令实现
- linux cp 命令的实现
- 简单cp命令的实现
- linux自己编写的 cp 命令
- APUE课后习题4.6
- apue 习题4.6参考答案
- APUE源代码的使用方法
- CP命令的较完整的实现
- cp命令的介绍与实现
- 家书
- 再见,CSDN
- Windows应用程序的退出
- C++线程安全的单例模式实现
- losetup: /dev/loop0: failed to set up loop device: No such device or address
- APUE习题4.6源代码----实现自己的简易 cp 命令
- Node.js模块 require和 exports
- 编程珠玑之第二章习题5
- android 如何判断有没有网络
- C语言结构体用法
- 【mongodb】安装及常用命令
- JDWP了解 与解决 ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)异常
- 【JavaIO】Java内存流
- 大数据——nutch1.8+solr 4 配置过程+ikanalayzer2012 中文分词器