title: C++ non null terminated char array outputting
body: <p>I was trying to output a not null terminated char array to a file.</p>
  
  <p>Actual thing is, I am receiving packets and then printing their fields.</p>
  
  <p>Now as these fields are not null terminated, for example, a data segment which has size of 512 but may or may not be completely occupied.</p>
  
  <p>When I write this data to a file I am using simple &lt;&lt; overloaded function which does not know any thing about actual data and only looks for termination of data segment.</p>
  
  <p>So, how can I tell the output function to write only this much <strong>specific number of bytes</strong>?</p>
  
  <p>Instead of using something like this which is expensive to call each time:  </p>
  
  <pre><code>enter code here  
  
  bytescopied = strncpy(dest, src, maxbytes);
  
  if (bytescopied &lt; 0) { // indicates no bytes copied, parameter error
  
      throw(fit);          // error handler stuff here
  
   } else if (bytescopied == maxbytes) {
  
      dest[maxbytes-1] = '\0';   // force null terminator
  
  }
  </code></pre>