- 论坛徽章:
- 0
|
下面就是这个程序, GetPkgValue 只能调用一次, 这样得到的值是正确的, 但调用第二次时 就错了, 后来发现在第一次调用中 入口数组buf 的值被变了。 ???????
---------------------------
#include <stdio.h>;
#include <stdlib.h>;
#include <string.h>;
int GetPkgValue( char *buf, char *tag_name, char value[]);
void main( void )
{
char buf[2048]= "{1:00054101000000000016 2003071517535810020030526}{2:1000000 }{3::30A:20030526:32A:RMB000000510385484:011:102521009993:52A:102521009993:CC4:102521009993:50C:06205729409859661382276023334095:50A:客户一 :50B:中华人民共和国:012:313161000011:58A:313161001011:CC5:313161001011:59C:58848850601918703254642093075515:59A:客户六 :59B:中华人民共和国:CEF:40:0BC:00000013:010:5210:0B9:1610:72A:自动生成的测试实例100}";
char tag_name[10]="30A";
char value[200];
GetPkgValue( buf, tag_name, value);
printf("---%s----%s---\n", tag_name, value );
memset( value, 0x00, sizeof( value) );
strcpy( tag_name, "32A"
GetPkgValue( buf, tag_name, value);
printf("---%s----%s---\n", tag_name, value );
}
int GetPkgValue( char *buf, char *tag_name, char value[])
{
char *p=NULL;
int i=0;
char *f=NULL;
int returnVal = 1 ;
p = strstr( buf, "{3:" );
if ( p==NULL )
{ printf(" ----GetPkgValue error occur \n" );
return returnVal;
}
p+=3;
while(1)
{
if( i==0 ) f=strtok( p, ":}"
else f=strtok( NULL, ":}"
if(!f) break;
if( i%2 == 0 )
if( strncmp( tag_name, f, 3)==0 )
{ strcpy( value, strtok( NULL, ":}");
returnVal = 0;
break;
}
i++;
}
return returnVal;
} |
|