title: C++ How to write block of strings to file?
body: <p>I have two really big numbers (max 23 digits/each) and I'd like to print them into a file as fast as it can be done. The lines in the file should look like: </p>
  
  <blockquote>
    <p>number1[space]number2[\n] eg. 123456789999999 99999965454644</p>
  </blockquote>
  
  <p>My approach is collecting the lines into one big buffer, and when the buffer is full, write it into the file, using fwrite(). </p>
  
  <p>The biggest problem is, that I don't know how to create one string from those 2 numbers and save them into one string/char array. As I mentioned the length of the string is variable, it can be only 3 characters or even 50.</p>
  
  <pre><code>const int OUT_BUFFER_SIZE = 65536;
  string out_buffer[OUT_BUFFER_SIZE];
  int out_size = 0;
  FILE* output_file = fopen("result.out", "w");
  
  void print_numbers(FILE* f, long long num1, long long num2)
  {
    const int ELEMENT_SIZE = 50;
  
    char line[ELEMENT_SIZE];
    sprintf(line, "%lld %lld\n", num1, num2);
    out_buffer[out_size] = line;
    out_size++;
  
    if (out_size == OUT_BUFFER_SIZE)
    {
       fwrite(out_buffer, ELEMENT_SIZE, OUT_BUFFER_SIZE, f);
    }
  }
  
  fclose(output_file);
  </code></pre>
  
  <p>Is there any other way how to solve this problem? Thanks.</p>
  
  <p><strong>EDIT1:</strong>
  I've tried two addtional approaches:</p>
  
  <p>1) write numbers immediately into the file using <code>fprintf(f, "%lld %lld\n", num1, num2);</code></p>
  
  <p>2) using ofstream exactly the same way as @somnium mentioned.</p>
  
  <p>I did measure both of them, and surprisingly, the 1) approach is 2x faster than the second. </p>