title: Copying a file line by line into a char array with strncpy
body: <p>So i am trying to read a text file line by line and save each line into a char array.</p>
  
  <p>From my printout in the loop I can tell it is counting the lines and the number of characters per line properly but I am having problems with <code>strncpy</code>.  When I try to print the data array it only displays 2 strange characters.  I have never worked with <code>strncpy</code> so I feel my issue may have something to do with null-termination.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main(int argc, char* argv[])
  {
      FILE *f = fopen("/home/tgarvin/yes", "rb");
      fseek(f, 0, SEEK_END);
      long pos = ftell(f);
      fseek(f, 0, SEEK_SET);
      char *bytes = malloc(pos); fread(bytes, pos, 1, f);
      int i = 0; 
      int counter = 0; 
      char* data[counter]; 
      int length; 
      int len=strlen(data); 
      int start = 0;
      int end = 0;
  
      for(; i&lt;pos; i++)
      {
          if(*(bytes+i)=='\n'){
              end = i;
              length=end-start;
              data[counter]=(char*)malloc(sizeof(char)*(length)+1);
              strncpy(data[counter], bytes+start, length);
              printf("%d\n", counter);
              printf("%d\n", length);
              start=end+1;
              counter=counter+1;
          }
      }
      printf("%s\n", data);
      return 0;
  }
  </code></pre>