title: The fastest way to save graph to file in C
body: <p>I have such a problem: I need to save a large graph with > million edges to txt file. Each edge is represented with a structure containing 3 integers: from, to, cost. My task is to write a program that will fastly save the whole graph to txt file in such format: "from to cost\n".</p>
  
  <p>I am interested in the method, how to do that.
  My idea is in creating a huge buffer of chars, where I add each digit to the buffer without the need to reverse then (first of all I get the number of digits of each integer, then add each digit to the buffer, then I add whitespace/new line symbol and do it until the last number is added.</p>
  
  <p>Then I save the whole buffer to file by using fwrite() function.</p>
  
  <p>Despite the fact that this method is relatively fast, I saw programs that do it faster. My question is: do you know more efficient way to implement this program in order to get faster results?</p>
  
  <p>The program must be in C language.</p>
  
  <pre><code>typedef struct {
     int edge_start; 
     int edge_count; 
     int parent; 
     int cost; 
  } node_t;
  
  typedef struct {
     graph_t *graph;
     node_t *nodes;
     int num_nodes; 
     int start_node; 
  } dijkstra_t;
  </code></pre>
  
  <p>The function to get the number of digits:</p>
  
  <pre><code>int getNumberOfDigitsBig(int x) {
    if (x &gt;= 10000) {
          if (x &gt;= 10000000) {
              if (x &gt;= 100000000) {
                  if (x &gt;= 1000000000)
                      return 9;
                  return 8;
              }
              return 7;
          }
          if (x &gt;= 100000) {
              if (x &gt;= 1000000)
                  return 6;
              return 5;
          }
          return 4;
      }
      if (x &gt;= 100) {
          if (x &gt;= 1000)
              return 3;
          return 2;
      }
      if (x &gt;= 10)
          return 1;
      return 0;
  }
  </code></pre>
  
  <p>Save function:</p>
  
  <pre><code>    const dijkstra_t *const dij = (dijkstra_t*)dijkstra;
  
      if (dij) {
      FILE *f = fopen(filename, "w");
      if (f) {
  
      int numberOfNodes = dij-&gt;num_nodes;
      long bufferLength = numberOfNodes * (9 * 3 + 3);
      buffer = (char *)malloc(bufferLength + 1);
      long bufferCounter = 0;
  
      int number;
      // printf("i = %d\n", number);
      int counter;
      int digits;
  
      buffer[bufferCounter++] = '0';
      buffer[bufferCounter++] = ' ';
      buffer[bufferCounter++] = '0';
      buffer[bufferCounter++] = ' ';
      buffer[bufferCounter++] = '-';
      buffer[bufferCounter++] = '1';
      buffer[bufferCounter++] = '\n';
  
      for(int i = 1; i &lt; numberOfNodes; i++) {
          const node_t *const node = &amp;(dij-&gt;nodes[i]);
  
          number = i;
  
           digits = getNumberOfDigits(number);
           counter = bufferCounter;
  
           do {
             buffer[counter + digits] = ZERO + number % 10;
             --digits;
             ++bufferCounter;
           } while(number /= 10);
  
         buffer[bufferCounter++] = ' ';
  
         number = node-&gt;cost;
  
         if(number != -1) {
  
           digits = getNumberOfDigitsBig(number);
  
           counter = bufferCounter;
           do {
             buffer[counter + digits] = ZERO + number % 10;
             digits = digits - 1;
             bufferCounter = bufferCounter + 1;
           } while(number /= 10);
         } else {
           buffer[bufferCounter++] = '-';
           buffer[bufferCounter++] = '1';
  
         } 
  
        buffer[bufferCounter++] = ' ';
  
          buffer[bufferCounter++] = ' ';
  
          number = node-&gt;parent;
  
          if(number != -1) {
  
              digits = getNumberOfDigitsBig(number);
  
              counter = bufferCounter;
              do {
                  buffer[counter + digits] = ZERO + number % 10;
                  --digits;
                  ++bufferCounter;
              } while(number /= 10);
          } else {
                buffer[bufferCounter++] = '-';
                buffer[bufferCounter++] = '1';
  
          }
  
          buffer[bufferCounter++] = '\n';
  
      }
  
    fwrite(buffer, 1, bufferCounter, f);
  
    ret = fclose(f) == 0;
    free(buffer);
    }
   }
  </code></pre>
  
  <p>Thanks for attention. </p>