- 论坛徽章:
- 0
|
如何在C程序中去掉报文中^M!(急)
请各位高手看看我的代码错在哪里,或着如何写更合适!
int YDCL_Recv (int fd, char * buff)
{
int nbytes, result;
int templen,reflag;
char *p;
char tmp[5];
FILE *electfp;
char fpwjmc[30];
int len55,iii;
char *strbuff;
char *strtmp;
char *strbuff_tmp;
char dlczy[5];
char jylx[3];
strbuff=(char *)malloc(8400);
strtmp=(char *)malloc(8400);
strbuff_tmp=(char *)malloc(8400);
REFLAG:
memset(strbuff_tmp,0,8400);
memset(tmp,0,5);
nbytes = 0;//接收长度
do{
result = recv (fd, tmp+nbytes, 4-nbytes, 0);
nbytes += result;
} while ((nbytes<4)&&(result>;0)&&(nbytes>;0));
if(result<0)return(-1);
templen = atoi (tmp);
reflag=atoi(tmp);
if (templen == 0) return (0);
memset(tmp,0,5);
nbytes = 0;
do {
result = recv (fd, strbuff_tmp+nbytes, templen-nbytes, 0);
nbytes += result;
} while ((nbytes<templen)&&(result>;0)&&(nbytes>;0));
if(result<0)return(-1);
if (nbytes != templen)
{
return (-1);
}
/***************以下 处理
Begin***************/
strcat(strbuff,buff);
if (reflag==0) goto REFLAG;
free(strbuff_tmp);
strncpy(jylx,strbuff_tmp+2,2);
if(strcmp(jylx,"11",)==0)
{
strncpy(dlczy,strbuff+7,4);
sprintf(fpwjmc,"log/elect%s.prn",dlczy);
electfp=fopen(fpwjmc,"w"
if (electfp==NULL) return(1001);
len55=strlen(strbuff);
for (iii=0;iii<len55;iii++)
{
if (strbuff[iii]!=0x0a&&strbuff[iii]!=0x0d) fprintf(electfp,"%c",strbuff[iii]);
if (strbuff[iii]==0x0a) fprintf(electfp,"\n"
}
memset(tmp,0,5);
fseek(electfp,SEEK_SET,0);
fread(tmp,len55+1,1electfp);
strcat(buff,tmp);
fclose(electfp);
memset(tmp,0,5);
}
else
strcat(buff,strbuff_tmp);
/***************处理
完成END**************/
nbytes=strlen(buff);
return (nbytes);
} |
|