title: R truncate a text file at an index
body: <p>I have a text file <code>output.txt</code>. I would like to keep only the first <code>n</code> (for example, 1,000, known to me, surely no larger than the file size) characters and truncate the rest. If the file was small, I could do</p>
  <pre class="lang-r prettyprint-override"><code>n &lt;- 1000   # Just an example. I keep track of this. No worries.
  f &lt;- 'output.txt'
  cat(readChar(f,pmin(n,file.info(f)$size)), file=f, append=FALSE)
  cat(&quot;keep going&quot;, file=f, append=TRUE)
  </code></pre>
  <p>but the file can sometimes be very large and the above is inefficient. I am looking for something like the following C example:</p>
  <pre class="lang-c prettyprint-override"><code>long n = 1000L;
  FILE *fp = fopen(&quot;output.txt&quot;, &quot;w+&quot;);
  fseek(fp, n, SEEK_SET);   /* this part */
  fputs(&quot;keep going&quot;, fp);
  </code></pre>
  <p>Thanks.</p>