需要把各种类型的数据整合到同一个字符串中的时候,我想到了用printf的近亲——sprintf,后来发现sprintf整合的结果总是不正确,下面上代码片段
char text[1];
/* print ip addresses and udp ports */
sprintf(text,"%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d",
ih->saddr.byte1,
ih->saddr.byte2,
ih->saddr.byte3,
ih->saddr.byte4,
sport,
ih->daddr.byte1,
ih->daddr.byte2,
ih->daddr.byte3,
ih->daddr.byte4,
dport);
ui->listWidget->addItem(text);
char sip[10],sp[10],dip[10],dp[5];
sprintf(sip,"%d.%d.%d.%d",
ih->saddr.byte1,
ih->saddr.byte2,
ih->saddr.byte3,
ih->saddr.byte4);
sprintf(dp,"%d",sport);
sprintf(dip,"%d.%d.%d.%d",
ih->daddr.byte1,
ih->daddr.byte2,