找到你要的答案

Q:ANSI C : snprintf allocating issue

Q:ANSI C:snprintf分配问题

I encounter this issue when using the snprintf() function. I want to concat multiple strings.

I have a struct msg defined as

struct timerMessage { 
  char txncode[4]; 
  char date[8]; 
  char time[6]; 
  char mac[16]; 
  char userid[32]; 
  char instid[9]; 
  char merchcode[2]; 
  char seqno[3]; 
  char data[100]; 
} msg;

with these values:

{txncode = "9902", date = "08082015", time = "172936", mac = ' ' <repeats 16 times>, 
  userid = ' ' <repeats 32 times>, instid = "         ", merchcode = "  ", seqno = "001", 
  data = "FILE1TEST11|1P", '\000' <repeats 85 times>}

and this is how I concat the strings:

snprintf(msgbuffer,sizeof(msgbuffer),"%s%s%s%s%s%s%s%s%s",msg.txncode,
msg.date,msg.time,msg.mac,msg.userid,msg.instid,msg.merchcode,msg.seqno,msg.data);

UPDATE: MCVE

struct msg

   char txncode[4] ,date[8], time[6], mac[16], userid[32], instid[9], merchcode[2], seqno[3], data[100];

var

   sprintf(msg.txncode,  "%s","9901");
    sprintf(msg.date,     "%002d%002d%002d",month,day,year);
    sprintf(msg.time,     "%002d%002d%002d",hr,min,sec);
    sprintf(msg.mac,      "% 16s",          " ");
    sprintf(msg.userid,   "% 32s",          " ");
    sprintf(msg.instid,   "% 9s",           " ");
    sprintf(msg.merchcode,"% 2s",           " ");
    sprintf(msg.seqno,"%003d",1);
    snprintf(msg.data,sizeof(msg.data),"%s|%s","FILE1TEST11","1P");

the result of concatenation:

"990208082015172936", ' ' (repeats 59 times), "001FILE1TEST11|1P0808"

the result should be:

"990208082015172936", ' ' (repeats 59 times), "001FILE1TEST11|1P"

我遇到这个问题时使用snprintf()功能。我想连接多个字符串。

我有一个定义为struct味精

struct timerMessage { 
  char txncode[4]; 
  char date[8]; 
  char time[6]; 
  char mac[16]; 
  char userid[32]; 
  char instid[9]; 
  char merchcode[2]; 
  char seqno[3]; 
  char data[100]; 
} msg;

这些值:

{txncode = "9902", date = "08082015", time = "172936", mac = ' ' <repeats 16 times>, 
  userid = ' ' <repeats 32 times>, instid = "         ", merchcode = "  ", seqno = "001", 
  data = "FILE1TEST11|1P", '\000' <repeats 85 times>}

这就是我的连接字符串:

snprintf(msgbuffer,sizeof(msgbuffer),"%s%s%s%s%s%s%s%s%s",msg.txncode,
msg.date,msg.time,msg.mac,msg.userid,msg.instid,msg.merchcode,msg.seqno,msg.data);

更新:MCVE

struct味精

   char txncode[4] ,date[8], time[6], mac[16], userid[32], instid[9], merchcode[2], seqno[3], data[100];

VaR

   sprintf(msg.txncode,  "%s","9901");
    sprintf(msg.date,     "%002d%002d%002d",month,day,year);
    sprintf(msg.time,     "%002d%002d%002d",hr,min,sec);
    sprintf(msg.mac,      "% 16s",          " ");
    sprintf(msg.userid,   "% 32s",          " ");
    sprintf(msg.instid,   "% 9s",           " ");
    sprintf(msg.merchcode,"% 2s",           " ");
    sprintf(msg.seqno,"%003d",1);
    snprintf(msg.data,sizeof(msg.data),"%s|%s","FILE1TEST11","1P");

连接结果:

"990208082015172936", ' ' (repeats 59 times), "001FILE1TEST11|1P0808"

结果应该是:

"990208082015172936", ' ' (repeats 59 times), "001FILE1TEST11|1P"
answer1: 回答1:

You have only one issue here. In all the character arrays you are filling all the available position with values and none of the position can contain NULL ('/0') character. Modify your structure by adding one character in each character array as shown below and you'll get the desired result.

  3 struct timerMessage {
  4   char txncode[5];
  5   char date[9];
  6   char time[7];
  7   char mac[17];
  8   char userid[33];
  9   char instid[10];
 10   char merchcode[3];
 11   char seqno[4];
 12   char data[100];
 13 } msg;

这里只有一个问题。在所有字符数组中,你用值填充所有可用的位置,并且没有一个位置可以包含null(“/ 0”)字符。修改您的结构,在每个字符数组中添加一个字符,如下所示,您将得到期望的结果。

  3 struct timerMessage {
  4   char txncode[5];
  5   char date[9];
  6   char time[7];
  7   char mac[17];
  8   char userid[33];
  9   char instid[10];
 10   char merchcode[3];
 11   char seqno[4];
 12   char data[100];
 13 } msg;
c  printf