linux copy file

int cpfile_udisk(char *file_src, char *file_dst)

{

FILE *from_fd, *to_fd;

int cnt_from = -1;

int cnt_to = -1;

static char buf[1024 * 200];

int count = 0;

/* 创建目的文件 */

to_fd = fopen(file_dst, "wb");

if (NULL == to_fd) {

printf("fopen %s err dst %d %s\n", file_dst, errno, strerror(errno));

return -1;

}

/* 打开源文件 */

from_fd = fopen(file_src, "rb");

if (NULL == from_fd) {

fclose(to_fd);

printf("fopen %s err src %d %s\n", file_src, errno, strerror(errno));

return -1;

}

// 从源文件中读数据到buf,再将buf的数据写到目标文件中

while (cnt_from = fread(buf, sizeof(char), sizeof(buf), from_fd)) {

if (0 > (cnt_to = fwrite(buf, sizeof(char), cnt_from, to_fd))) {

printf("cnt %d write err %d %s\n", count, errno, strerror(errno));

}

count++;

}

printf("%d %d feof %d\n", count, ferror(from_fd), feof(from_fd));

fclose(from_fd);

fclose(to_fd);

return 0;

}

int cpfile_udisk(char *file_src, char *file_dst)

{

int from_fd;

int to_fd;

int cnt_from = -1;

int cnt_to = -1;

static char buf[1024 * 100];

int count = 0;

/* 创建目的文件 */

to_fd = open(file_dst, O_RDWR|O_CREAT|O_TRUNC, 0666);

if (0 > to_fd) {

printf("open %s err dst %d %s\n", file_dst, errno, strerror(errno));

return -1;

}

/* 打开源文件 */

from_fd = open(file_src, O_RDONLY);

if (0 > from_fd) {

close(to_fd);

printf("open %s err src %d %s\n", file_src, errno, strerror(errno));

return -1;

}

// 从源文件中读数据到buf,再将buf的数据写到目标文件中

while (cnt_from = read(from_fd, buf, sizeof(buf))) {

if (0 > (cnt_to = write(to_fd, buf, cnt_from))) {

printf("cnt %d write err %d %s\n", count, errno, strerror(errno));

}

count++;

}

printf("100k %d\n", count);

close(from_fd);

close(to_fd);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值