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;
}