title: snprintf does not stores last character in buffer?
body: <p>I have written a simple small program to read a file with following format using snprintf, </p>
  
  <pre><code>skip first 15 chars , next 9 chars are sequence number, next 2 char is message and so on.   
  </code></pre>
  
  <p>I am interested in sequence number and message i.e. from char no 16 to 26;</p>
  
  <p>below is the program. It does not reads last char for each field. It reads 8 chars instead of 9 for sequence number and and 1 byte instead of 2 for message.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  
  typedef struct
  {
    char seqno[9];
    char msg[2];
  }Header_T;
  
  int main()
  {
    char buf[64]={'\0'};
    FILE *fp;
    int i = 0;
    Header_T hdr1;
    int skipbytes = 15;
  
    fp=fopen("asdf", "r");
    if (fp == NULL)
    {
      printf("FILE OPEN ERROR\n");
    }
    printf("--sequence--msg--\n");
    while( fgets( buf, sizeof(buf), fp ) != NULL )
    {
      i=skipbytes;
      snprintf(hdr1.seqno, 9, "%s", (buf+i));
      i+=sizeof(hdr1.seqno);
      snprintf(hdr1.msg, 2, "%s", (buf+i));
      i=0;
  
      printf("--%s--%s--\n", hdr1.seqno, hdr1.msg);
      memset(buf, '\0', 64 );
    }
  fclose(fp);
  return 0 ;
  }
  </code></pre>
  
  <hr>
  
  <p>part of file contents is as follows  </p>
  
  <pre><code>201301082323458000000001H QB234
  201301082323558000000002J QB234
  201301082323658000000003N QB234
  201301082323758000000004JRQB234
  201301082333458000000010JSQB234
  </code></pre>
  
  <p>so expected output is </p>
  
  <pre><code>--sequence--msg--
  --000000001--H --
  --000000002--J --
  --000000003--N --
  --000000004--JR--
  --000000010--JS--
  </code></pre>
  
  <p>But Instead I am getting output as </p>
  
  <pre><code>--seqno--msgtype--
  --00000000--H--
  --00000000--J--
  --00000000--N--
  --00000000--J--
  --00000001--J--
  </code></pre>
  
  <p>Can anyone explain this behavior and how to fix it?</p>
  
  <p>Instead of <strong>snprintf</strong> I tried the same program using <strong>for loop character-by-character assignment</strong> and program works fine; but for that I need to add some fillers for byte alignment in structure.</p>
  
  <p>I also tried using <strong>pragma pack()</strong> but it also makes no difference.</p>
  
  <p>I am using gcc 4.4.3 on ubuntu 64 bit machine </p>