title: C: simultaneous reading from and writing to file
body: <p><strong>What i would like to do:</strong>
  Read bits from one file (input file), and write these (with some probability) inverted bits to other file (output file).</p>
  
  <p><strong>What is the problem:</strong>
  Probability idea seem not to be working properly. And more importantly, output file always contains more characters then the original input file, while they should contain equal number of characters.</p>
  
  <p><em>In this code sample, instead of inverted bits i have put 'x' and 'y', so that it is more obvious that output file contains more characters</em></p>
  
  <p><em>INPUT file</em>: 01001</p>
  
  <p><em>OUTPUT file</em>: xyxxxyx</p>
  
  <p><strong>The code:</strong></p>
  
  <pre><code>void invert_bits(FILE **input, FILE **output, double prob){
  srand(clock());
  char symbol;
  while((symbol = getc(*input)) != EOF){
      double result = rand()/RAND_MAX;
      if(result &lt; prob){
          if(simbol == '0'){
              char bit = 'x';
              fprintf(*output, &amp;bit);
          }
          else{
              char bit = 'y';
              fprintf(*output, &amp;bit);
          }
      }else{
          fprintf(*output, &amp;symbol);
      }
  }
  </code></pre>
  
  <p>}</p>