经常遇到需要拷贝文件内容的时候,反复造轮子不如直接用轮子。
int copy_file(int read_fd, int write_fd)
{
char buf[64] = {0};
int length = 0;
int pos = 0;
int ret = 0;
while((length = READ(read_fd, buf, sizeof(buf))) > 0)
{
while(length)
{
ret = write(write_fd, buf + pos, length);
if(ret < 0)
{
LOG_INFO(LOG_ERROR, "write error \n");
return -1;
}
pos += ret;
length -= ret;
}
MEMSET(buf, 0, sizeof(buf));
pos = 0;
}
if(length < 0)
{
LOG_INFO(LOG_ERROR, "read error \n");
return -1;
}
return 0;
}