Linux系统调用实现文件复制

论坛 期权论坛 脚本     
匿名技术用户   2021-1-6 07:39   53   0
#include <stdio.h> #include <fcntl.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> int main( int argc, char *argv []){ int fd1, fd2; int count, count2; char buffer[ 1024 ] = {0} ; char *ptr ; if (argc != 3 ) { printf ( "Usage: %s file1 file2\n" , argv[ 0 ]); return 1 ; } if (- 1 == (fd1 = open (argv[ 1 ], O_RDONLY))) { perror( "open file1\n" ); close (fd1); return 2 ; } if (- 1 == (fd2 = open (argv[ 2 ], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR))) { perror( "open file2\n" ); close (fd2); return 3 ; } while (count = read (fd1, buffer, 1024 )) { if (- 1 == count) { perror( "read" ); return 4 ; } ptr = buffer; int count2 = write (fd2, ptr, count); if (- 1 == count2) { perror( "write\n" ); return 5 ; } memset(buffer, 0 , 1024 ); } close (fd1); close (fd2); return 0 ;}
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP