C: strncpy unexpectedly truncating string
<p>I am reading a file line by line, where each line is of the format:</p>

<p>"  number1  \t number2".</p>

<p>I am using strtok and strncpy to split and then store the two values as needed.
However, I find that after strncpy, number1 is truncated by one digit.</p>

<p>Any ideas of why this could be and how to solve it?</p>

<p>For simplicity, I have hard coded a line_of_text to simulate the problem.</p>

<p>Thanks!</p>

<p>Code:</p>

<pre><code>#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;


int main()
{
  char line_of_text[80] = "  18306000  \t 100\n"; 
  // NB: There's a \t between the two values, the rest are spaces.

  char* token;

  int digits_per_num = 9;

  char first_token[digits_per_num];
  char second_token[digits_per_num];

  token = strtok (line_of_text, "\t");

  printf("first token: %s\n", token);

  if (token != NULL)
    {
      strncpy (first_token, token, digits_per_num);
    }

    token = strtok (NULL, ",\t\n");
    if (token != NULL)
    {
      strncpy (second_token, token, digits_per_num);
    }


    printf("first token copy: %s\n", first_token);
    printf("second token copy: %s\n", second_token);

}
</code></pre>

<p>Output:</p>

<pre><code>first token:  18306000
first token copy: 1830600&lt;junk&gt;
second token copy: 100
</code></pre>