//出错返回-1,成功返回写入的长度
int writeFile(const char *path, void *buf, unsigned int len, int offset)
{
int ret = 0;
FILE *fp = NULL;
if(NULL==(fp = fopen(path, "rb+")))
{//以可读写方式打开文件,该文件必须存在。
if(NULL==(fp = fopen(path, "wb+")))
{//打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。
printf_err("fopen(%s) failed:%s\n", path, strerror(errno));
return -1;
}
}
if(offset>0)
{
if(-1==fseek(fp, offset, SEEK_SET))
{
printf_err("fopen(%s) failed:%s\n", path, strerror(errno));
fclose(fp);
return -1;
}
}
ret = fwrite(buf, 1, len, fp);
if(0!=ferror(fp))
{
printf_err("fread(%s) failed:%s\n", path, strerror(errno));
ret=-1;
}
fclose(fp);
return ret;
}
//出错返回-1,成功返回读取的长度
int readFile(const char *path, void *buf, unsigned int len, int offset)
{
int ret = 0;
FILE *fp = NULL;
if( NULL==(fp=fopen(path, "rb")) )
{//以只读方式打开文件,该文件必须存在。
printf_err("fopen(%s) failed:%s\n", path, strerror(errno));
return -1;
}
if(offset>0)
{
if(-1==fseek(fp, offset, SEEK_SET))
{
printf_err("fopen(%s) failed:%s\n", path, strerror(errno));
fclose(fp);
return -1;
}
}
ret = fread(buf, 1, len, fp);
if(0!=ferror(fp))
{
printf_err("fread(%s) failed:%s\n", path, strerror(errno));
ret=-1;
}
fclose(fp);
return ret;
}
char l_readBuf[1024];
uint32_t l_offset = 0;
uint32_t l_readLen = 0;
uint32_t l_ret = 0;
while(l_offset < l_statbuf.st_size)
{
l_readLen = l_statbuf.st_size - l_offset;
l_readLen = l_readLen>1024?1024:l_readLen;
l_ret = readFile(l_picPath, l_readBuf, l_readLen, l_offset);
if(l_ret <=0)
{
//printf("read err [%d]\n", l_ret);
break;
}
if(l_offset==0)
{
writeFile(l_headPath, l_readBuf, l_ret, l_offset+128);
}
else
{
writeFile(l_headPath, l_readBuf, l_ret, l_offset);
}
l_offset += l_ret;
}