看看下面一段代码:
char *newbuf;
long int flen,read_size;
FILE *pFile=fopen("./get_time.cpp","r");
fseek(pFile, 0, SEEK_END);
flen=ftell(pFile);
printf("flen=%d/n",flen);
newbuf=(char *)malloc(flen+1);
fseek(pFile,0,SEEK_SET);
while((read_size=fread(newbuf,1,1024,pFile))>0)
{
if((sendbytes=send(client_fd,newbuf,read_size,0))==-1)
{
perror("send");
exit(1);
}
printf("sendbytes=%d/n",sendbytes);
printf("read_size=%d/n",read_size);
printf("*****/n");
fflush(pFile);
}
printf("read_size=%d/n",read_size);
fclose(pFile);
假如pfile指向的文件在1025以上,就会出现问题。
比如为2000,第一次读取返回值read_size为1024,第2次读取read_size就为0.所以要么一次性读取完,要么就不用fread,改用read函数。今天碰到的问题,以后注意一下。