fastest way to write integer to file in C
<p>I am doing a homework to programming in C. Bonus points are for quick writing to the file in there upload test system.</p>

<p>I am trying to write a number of lines each comprising three space delimited decimal integer strings and then '\n' in file. The problem is, that fprintf is too slow (their reference time is more or less 1/3 faster).</p>

<p>I have tried a lots of possibilities (everything is in one for loop).
fprintf (too slow):</p>

<pre><code>fprintf(f, "%d %d %d\n", a[i], b[i], c[i]);
</code></pre>

<p>converting to string and then put the string into it - even worse:</p>

<pre><code>sprintf(buffer, "%d", a[i]); //or: _itoa(_itoa(a[i], buffer, 10);
fputs(buffer, f);
fputc(' ', f);
</code></pre>

<p>is there any quick way to write integer numbers to simple text file (.txt) (the last solution has time 220ms, reference is 140ms for you to picture the time)? I have been trying and googling as hell, but nothing is working. But if the time is this short, there has to be some way!</p>

<p>PS: The numbers are integers all the time, size is 4 bytes, all the time in format:</p>

<pre><code>a0 b0 c0
a1 b1 c1
a2 b2 c2
a3 b3 c3
etc...
</code></pre>

<p>More info: When I send the solution, I send only two files: file.h and file.c. No main etc... so everything is in their optimization. The solution should be in commands/algorithm (even in the description of the problem is statement, that fprintf is too slow and we should try something else to speed things up).</p>

<p>Thank you for everything!</p>

<p>Edit: since you want the whole code, here it is:</p>

<pre><code>void save(const str_t * const str, const char *name)
{
  FILE* f;
  int i;

  if(str-&gt;cnt == 0)
      return;

  f = fopen(name, "w");
  if(f == NULL)
      return;

  for(i = 0; i &lt; str-&gt;cnt; i++)
  {
      fprintf(f, "%d %d %d\n", str-&gt;a[i], str-&gt;b[i], str-&gt;c[i]);
  }
  fclose(f);
}
</code></pre>